手把手重复类似《Nature》论文的图!多个组别画图,xy轴互换,legend调整~

学术   科学   2024-03-08 10:19   浙江  
今天来画类似《Nature》上论文[1] 中的Figure1b,为下图中的b:

Fig1b [1]


首先载入R包:

library(ggplot2) # 需提前安装install.packages("ggplot2")
第二步,创建作图用的数据:

set.seed(1
data <- data.frame(
  types = c(rep("BA.1", 111), rep("BA.2", 195), rep("Alpha", 18), rep("Delta", 117)),
  groups = c(rep("Persistent infection", 97), rep("Reinfection", 14),
             rep("Persistent infection", 167), rep("Reinfection", 28),
             rep("Persistent infection", 11), rep("Reinfection", 7),
             rep("Persistent infection", 106), rep("Reinfection", 11)),
  days = c(sample(26:180, 97, replace = T), # BA.1
           sample(26:75, 14, replace = T),
           sample(26:140, 167, replace = T), # BA.2
           sample(30:125, 27, replace = T), 200,
           sample(26:190, 11, replace = T), # Alpha
           sample(26:165, 7, replace = T),
           sample(26:130, 106, replace = T), # Delta
           sample(26:150, 11, replace = T)))

summary(data)

因为作图数据为小编根据原图随机创建,与原论文中数据不同,所以画出的点分布会不一样。

最后开始作图:

ggplot(data, aes(x = types, y = days)) +
  geom_point(aes(color = groups),
             size = 1,
             position = position_jitterdodge(dodge.width = 0.9,
                                             seed = 2,
                                             jitter.width = 0.3,
                                             jitter.height = 0.1)) +
  scale_y_continuous(breaks = c(50, 100, 150, 200),
                     limits = c(18, 200)) +
  scale_x_discrete(limits = c("Alpha", "Delta", "BA.1", "BA.2")) +
  scale_color_manual(values = c("mediumaquamarine", "plum3"),
                     limits = c("Reinfection", "Persistent infection")) +
  geom_hline(yintercept = 26, color = "grey") +
  geom_hline(yintercept = 56, color = "grey") +
  coord_flip() +
  theme_classic(base_size = 14) +
  theme(legend.position = "top",
        legend.justification = "right",
        axis.text = element_text(color = "black")) +
  guides(colour = guide_legend(override.aes = list(size = 3))) +
  labs(x = "",
       y = "Time between the earliest and latest samples (days)",
       color = "") +
  annotate("text", x = 4.2, y = 18, label = "28", size = 4) +
  annotate("text", x = 3.8, y = 18, label = "167", size = 4) +
  annotate("text", x = 3.2, y = 18, label = "14", size = 4) +
  annotate("text", x = 2.8, y = 18, label = "97", size = 4) +
  annotate("text", x = 2.2, y = 18, label = "11", size = 4) +
  annotate("text", x = 1.8, y = 18, label = "106", size = 4) +
  annotate("text", x = 1.2, y = 18, label = "7", size = 4) +
  annotate("text", x = 0.8, y = 18, label = "11", size = 4)

类似原论文中的图就轻松get


上述代码中,函数 geom_point() 添加一个几何图层,在此例中是点图。position = position_jitterdodge() 处理重叠的点:dodge.width = 0.9 控制两个亚组分别往两侧偏移;seed = 2 让随机可重复;jitter.width = 0.3 和 jitter.height = 0.1 给点添加额外的随机抖动。


画完类似《Nature》论文的图~~换上自己的数据,感觉离它又近了0.0000001步

好啦,今天的内容就到这里。如果有帮助,记得分享给需要的人

参考文献

[1] https://www.nature.com/articles/s41586-024-07029-4/figures/1

公众号的线上课程
1. 《R语言和统计新手课程》
2. 《回归:从入门到进阶》

统计咨询
《服务介绍和经典合作案例》

公众号核心成员的成果发表
《SCI医学1区影响因子9分论文》

公众号核心成员担任SCI杂志Associate Editor!
《JAD杂志Associate editor》
《Frontiers in Neuroscience, Frontiers in Neurology and Frontiers in Psychiatry杂志的神经退行性病变板块》

▌本文由R语言和统计首发
▌课程相关咨询可添加R师妹微信: kefu_rstats
▌编辑:June
▌邮箱:contact@rstats.cn
▌网站:www.rstats.cn
我们致力于让R语言和统计变得简单!



R语言和统计
我们定期更新与R有关的内容,比如R编程基础,作图,实用R包的解读,统计学基础知识,前沿的统计方法,机器学习等等。
 最新文章