直接使用ggplot的内置数据集’mpg’
library(ggplot2)
data(mpg, package="ggplot2")
ggplot(mpg, aes(cty, hwy)) +
geom_point(size=3,shape=23,aes(fill=drv))
花上面那张图,只需要两行代码就行了,这就是图层语言。接下来根据ggplot的逻辑优化。
添加拟合(smooth)、图片标题(ggtitle/ also labs)、轴标题(labs)、注释(annotate);可以在任意位置添加注释
ggplot(mpg, aes(cty, hwy)) +
geom_point(size=3,shape=23,aes(fill=drv)) +
geom_smooth(method = 'lm', color='orange') +
labs(title = "This is a figure by Wang.", tag = "(a)") +
# theme(plot.title = element_text(hjust = 0.5)) #图题居中
labs(y="cty",x="hwy1")+
annotate("text",x=10,y=48,label="p < 0.05")
p是斜体:
ggplot(mpg, aes(cty, hwy)) +
geom_point(size=3,shape=23,aes(fill=drv)) +
geom_smooth(method = 'lm', color='orange') +
labs(title = "This is a figure by Wang.", tag = "(a)") +
labs(y="cty",x="hwy1")+
annotate("text",x=10,y=48,label=expression(paste(italic("p")," < 0.05")))
现在的图看上去还行,但需要修改的地方还有很多。有时候还需要手动调节xy范围,这就用到scale。ggplot默认的颜色有点烂大街,需要手动调色。这需要对细节有所了解:
ggplot(mpg, aes(cty, hwy)) +
geom_point(size=3,shape=23,aes(fill=drv)) +
geom_smooth(method = 'lm', color='orange') +
labs(title = "This is a figure by Wang.", tag = "(a)") +
labs(y="cty",x="hwy1")+
annotate("text",x=10,y=48,label=expression(paste(italic("p")," < 0.05"))) +
scale_y_continuous(expand = c(0,0),limits = c(10,50))+
scale_x_continuous(expand = c(0,0),limits = c(8,35))+
scale_fill_manual(values =c('#F7BC08',"#E94335","#1194C2"))
设置主题,ggplot默认主题有点烂大街。可以用经典主题theme_classic()、theme_bw()
ggthemes包也内置了很多主题,感兴趣的可以试下:
ggplot(mpg, aes(cty, hwy)) +
geom_point(size=3,shape=23,aes(fill=drv)) +
geom_smooth(method = 'lm', color='orange') +
labs(title = "This is a figure by Wang.", tag = "(a)") +
labs(y="cty",x="hwy1")+
annotate("text",x=10,y=48,label=expression(paste(italic("p")," < 0.05"))) +
scale_y_continuous(expand = c(0,0),limits = c(10,50))+
scale_x_continuous(expand = c(0,0),limits = c(8,35))+
scale_fill_manual(values =c('#F7BC08',"#E94335","#1194C2")) +
theme_light()
最后调整图的细节,代码一经写好直接套用,字体字号全都修改好,直接在论文中使用,免得去AI手动修改。
windowsFonts(myFont = windowsFont("Times new roman"))
ggplot(mpg, aes(cty, hwy)) +
geom_point(size=3,shape=23,aes(fill=drv)) +
geom_smooth(method = 'lm', color='orange') +
labs(tag = "(a)") +
labs(y="cty",x="hwy1")+
annotate("text",x=10,y=48,family="myFont",size=5,label=expression(paste(italic("p")," < 0.05"))) +
scale_y_continuous(expand = c(0,0),limits = c(10,50))+
scale_x_continuous(expand = c(0,0),limits = c(8,35))+
scale_fill_manual(values =c('#F7BC08',"#E94335","#1194C2")) +
theme_light() +
theme(axis.title= element_text(size=20,color="black",
face="bold", family="myFont"),
axis.text = element_text(size=15,color="black",
face="bold", family="myFont"),
axis.line = element_line(color = "black",
size = 1, linetype = "solid"),
#线的类型粗细和颜色,这里的线只有两个轴
axis.ticks = element_line(size = 1),
axis.ticks.length = unit(5, "pt"),
legend.position = "top", #图例的位置
legend.text = element_text(size=15,color="black",face="bold", family="myFont"),
legend.title = element_blank()
)
对比一下最初的图和优化后的结果。
对比一下最初的图和优化后的结果。
优化后的图基本符合科研论文的绘图要求和出版规范,无论在美观程度 上还是格式等等。