ggplot科研图绘美化逻辑

直接使用ggplot的内置数据集’mpg’

library(ggplot2)
data(mpg, package="ggplot2")

ggplot(mpg, aes(cty, hwy)) +
  geom_point(size=3,shape=23,aes(fill=drv))

image-20221113223916124

花上面那张图,只需要两行代码就行了,这就是图层语言。接下来根据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")

image-20221113224011126

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")))

image-20221113224038278

现在的图看上去还行,但需要修改的地方还有很多。有时候还需要手动调节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"))

image-20221113224058515

设置主题,ggplot默认主题有点烂大街。可以用经典主题theme_classic()、theme_bw()

ggthemes包也内置了很多主题,感兴趣的可以试下:

image-20221113224107004

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()

image-20221113224133256

最后调整图的细节,代码一经写好直接套用,字体字号全都修改好,直接在论文中使用,免得去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()
       )

对比一下最初的图和优化后的结果。

image-20221113224333786image-20221113224224961

对比一下最初的图和优化后的结果。

优化后的图基本符合科研论文的绘图要求和出版规范,无论在美观程度 上还是格式等等。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地学万事屋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值