zl程序教程

您现在的位置是:首页 >  其他

当前栏目

跟着Nature学作图:R语言ggplot2箱线图/散点图完整示例

语言 示例 完整 Nature ggplot2 跟着 作图 散点图
2023-06-13 09:16:32 时间

论文

Graph pangenome captures missing heritability and empowers tomato breeding

https://www.nature.com/articles/s41586-022-04808-9#MOESM8

没有找到论文里的作图的代码,但是找到了部分做图数据,我们可以用论文中提供的原始数据模仿出论文中的图

今天的推文重复一下论文中的 Figure4d Figure4e 散点图和箱线图

image.png

箱线图示例数据集

image.png

作图代码

library(readxl)
dat01<-read_excel("data/20220711/41586_2022_4808_MOESM8_ESM.xlsx",
                  sheet = "Fig4e",
                  skip = 1)
dat01

library(ggplot2)

dat01$Type<-factor(dat01$Type,
                   levels = c("SNP","InDel","SV","SV array"))

ggplot(data=dat01,aes(x=Type,y=Accuracy))+
  
  geom_line(aes(group=Metabolic),
            color="grey")+
  geom_boxplot(aes(color=Type),
               fill="transparent")+
  geom_point(aes(color=Type))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        legend.position = "none")+
  scale_color_manual(values = c("#3288bd","#66c2a5",
                                "#f46d43","#f1226e"))+
  labs(x=NULL,y="Prediction accuracy") -> p1

p1

image.png

散点图作图代码

dat02<-read_excel("data/20220711/41586_2022_4808_MOESM8_ESM.xlsx",
                  sheet = "Fig4d",
                  skip = 1)
dat02

library(paletteer)
library(latex2exp)

ggplot(data=dat02,aes(x=accuracy_snp,y=accuracy_sv))+
  geom_point(aes(color=h2_snp))+
  scale_x_continuous(limits = c(0,0.5),
                     expand = expansion(mult = c(0,0)))+
  scale_y_continuous(limits = c(0,0.5),
                     expand = expansion(mult = c(0,0)))+
  geom_abline(slope = 1,intercept = 0,lty="dashed")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(),
        legend.position = c(0.8,0.3))+
  scale_color_paletteer_c(palette="grDevices::heat.colors",
                          direction = -1,
                          breaks=c(0.01,0.5,0.99),
                          labels=c(0,"0.50","1.00"),
                          name=TeX(r"(\textit{h}${^2}$)"))+
  labs(x="Accuracy of SNP",y="Accuracy of SV") -> p2

p2

image.png

拼图

library(patchwork)

p2+p1

image.png

示例数据和代码可以自己到论文中获取