#1.基础包 略显陈旧 了解一下一般不用
plot(iris[,1],iris[,3],col = iris[,5])
text(6.5,4, labels = 'hello') # 图例的位置
dev.off() #关闭画板
#2.ggplot2 中坚力量,语法有个性,扩展性强,推荐使用
library(ggplot2)
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
#3.ggpubr 新手友好型 ggplot2简化和美化 褒贬不一
library(ggpubr)
ggscatter(iris,
x="Sepal.Length",
y="Petal.Length",
color="Species")
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
# 作图数据 作什么图 xy轴
# 特殊语法:列名不带引号,行末写加号按回车自动缩进
#2.1 手动设置,需要设置为有意义的值
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, # mapping映射
y = Petal.Length),
color = "blue")
ggplot(data = iris) +
geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length),
size = 5, # 点的大小5mm
alpha = 0.5, # 透明度 50%
shape = 8) # 点的形状
#2.2 映射:按照数据框的某一列来定义图的某个属性
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))
## 映射color:根据某一列的内容分配颜色。手动设置:把图形设置为一或多个颜色,与数据内容无关
## Q1 能不能自行指定映射的具体颜色?注意写加号 映射和指定颜色必须都要写 十六进制颜色编码
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+
scale_color_manual(values = c("blue","grey","red"))
# 好用的配色R包 RColorBrewer ggsci paletteer
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species))+
scale_color_brewer(palette = "Set1")
## Q2 区分color和fill两个属性
### Q2-1 空心形状和实心形状都用color设置颜色
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 17) #17号,实心的例子
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 2) #2号,空心的例子
### Q2-2 既有边框又有内心的,才需要color和fill两个参数
ggplot(data = iris)+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length,
color = Species),
shape = 24,
fill = "black") #24号,双色的例子
# geom_开头的函数是画图函数,画出的是一个几何对象,一个图层,图层可以叠加
#局部设置和全局设置
## 局部设置
ggplot(data = iris) +
geom_smooth(mapping = aes(x = Sepal.Length,
y = Petal.Length))+
geom_point(mapping = aes(x = Sepal.Length,
y = Petal.Length))
## 全局设置 每个图层有特殊设置的地方可以单独写在他的括号里
ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+
geom_smooth()+
geom_point()
ggplot(data = iris,mapping = aes(x = Species, y = Sepal.Width, fill = Species))+
geom_boxplot()+
geom_point()
# 发现点图的所有点都在一条竖线上,存在很多重叠的点,失真
## 抖动的点图
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_boxplot()+
#geom_point(position = "jitter")
geom_jitter()
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_boxplot()+
geom_jitter()+
coord_flip() ## 翻转坐标系
ggplot(data = iris,mapping = aes(x = Species,
y = Sepal.Width,
fill = Species)) +
geom_boxplot()+
geom_jitter()+
theme_bw() ## 改主题,去掉灰色格子 theme_函数
好用的学习绘图网站STHDA
ggplot(data = iris,mapping = aes(x = Sepal.Width, y = Species))+
geom_violin(aes(fill = Species))+
geom_boxplot()+
geom_jitter(aes(shape = Species))
# 图层顺序与代码顺序有关
# ggpubr 搜代码直接用,基本不需要系统学习
# sthda上有大量ggpubr出的图
library(ggpubr)
p = ggboxplot(iris, x = "Species", y = "Sepal.Length",
color = "Species", shape = "Species",add = "jitter") # 代码很简单
p
# 添加显著性标记
my_comparisons <- list( c("setosa", "versicolor"),
c("setosa", "virginica"),
c("versicolor", "virginica") )# list打包向量
p + stat_compare_means(comparisons = my_comparisons,
aes(label = after_stat(p.signif)))
ggplot2 添加显著性标记方法类似,可参考以下内容https://zhuanlan.zhihu.com/p/644988686
#图片保存的三种方法
#1.基础包作图的保存
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off() # 关闭画板,不出图可能是没关闭画板
#2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave
p <- ggboxplot(iris, x = "Species",
y = "Sepal.Length",
color = "Species",
shape = "Species",
add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")
#3.eoffice包 导出为ppt,全部元素都是可编辑模式
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")
画图代码+你的数据+你解决问题的能力=你的图
后面分专题讲解
引用自生信技能树
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。