跟着Nature Medicine学画图:R语言散点图组合频率分布图的简单小例子

今天的推文内容来自论文 Clustering and superspreading potential of SARS-CoV-2 infections in Hong Kong, 期刊是 Nature Medicine

image.png

论文的数据代码公开,非常好的学习R语言的素材

数据代码地址 https://github.com/dcadam/covid-19-sse

今天的推文内容我们来学习一下论文中的figure4b ,散点图叠加频率分布图

image.png
首先是频率分布直方图

部分数据如下


image.png

做图用到的是最后一列数据

df1<-read.csv("example1.csv",header=T)
library(ggplot2)
ggplot(df1) +
  geom_histogram(aes(x = delay,y=..density..),  
                 fill = '#dedede', colour = "black", 
                 binwidth = 1) +
  scale_y_continuous("Frequency", expand = c(0,0), limits = c(0,0.20)) + 
  scale_x_continuous("Delay from onset-to-isolation of infector (days)", 
                     expand = c(0,0), 
                     limits = c(0,27), 
                     breaks = seq(0,27, by = 3)) +
  theme_classic() +
  theme(#aspect.ratio = 2, 
        legend.position = 'none')
image.png

这里新学到的知识点是
theme()函数里的aspect.ratio参数,这个参数可以控制整幅图占比,如果是0到1之间就是纵向的压缩,如果是1到2之间就是纵向的压缩,我们分别设置0.5和1.5看下效果

p0.5<-ggplot(df1) +
  geom_histogram(aes(x = delay,y=..density..),  
                 fill = '#dedede', colour = "black", 
                 binwidth = 1) +
  scale_y_continuous("Frequency", expand = c(0,0), limits = c(0,0.20)) + 
  scale_x_continuous("Delay from onset-to-isolation of infector (days)", 
                     expand = c(0,0), 
                     limits = c(0,27), 
                     breaks = seq(0,27, by = 3)) +
  theme_classic() +
  theme(aspect.ratio = 0.5, 
        legend.position = 'none')
p0.5
p1.5<-ggplot(df1) +
  geom_histogram(aes(x = delay,y=..density..),  
                 fill = '#dedede', colour = "black", 
                 binwidth = 1) +
  scale_y_continuous("Frequency", expand = c(0,0), limits = c(0,0.20)) + 
  scale_x_continuous("Delay from onset-to-isolation of infector (days)", 
                     expand = c(0,0), 
                     limits = c(0,27), 
                     breaks = seq(0,27, by = 3)) +
  theme_classic() +
  theme(aspect.ratio = 1.5, 
        legend.position = 'none')
cowplot::plot_grid(p0.5,p1.5,labels = c("p0.5","p1.5"))

image.png
接下来是散点图

散点图的部分数据如下

image.png
df2<-read.csv("example2.csv",header=T)
ggplot(df2) +
  geom_smooth(method = lm, aes(x=delay, y = n), color = "black", alpha = 0.1, size = 0.7) +
  geom_jitter(aes(x = delay, y = n, colour = cluster.risk), height = 0.3, width = 0.3) +
  scale_y_continuous("Secondary Cases / Infector", breaks = 1:11) +
  scale_x_continuous("Delay from onset-to-confirmation of infector (days)", 
                     expand = c(0,0),                     
                     limits = c(0,27), breaks = seq(0,27, by = 3)) +
  theme_classic() +
  theme(aspect.ratio = 1, legend.position = c(0.85, 0.85), legend.title = element_blank())  #colours are modified custom in post 

image.png

这里需要注意的是散点图他用到的函数是geom_jitter(),而没有用geom_point(),这两个函数的区别是如果两个点的坐标是一样的geom_jitter()函数也会将两个点分开,而geom_point()函数会将两个点重叠的画到一起

最后是拼图
p1<-ggplot(df1) +
  geom_histogram(aes(x = delay,y=..density..),  
                 fill = '#dedede', colour = "black", 
                 binwidth = 1) +
  scale_y_continuous("Frequency", expand = c(0,0), limits = c(0,0.20)) + 
  scale_x_continuous("Delay from onset-to-isolation of infector (days)", 
                     expand = c(0,0), 
                     limits = c(0,27), 
                     breaks = seq(0,27, by = 3)) +
  theme_classic() +
  theme(#aspect.ratio = 0.5, 
        legend.position = 'none')
p2<-ggplot(df2) +
  geom_smooth(method = lm, aes(x=delay, y = n), color = "black", alpha = 0.1, size = 0.7) +
  geom_jitter(aes(x = delay, y = n, colour = cluster.risk), height = 0.3, width = 0.3) +
  scale_y_continuous("Secondary Cases / Infector", breaks = 1:11) +
  scale_x_continuous("Delay from onset-to-confirmation of infector (days)", 
                     expand = c(0,0),                     
                     limits = c(0,27), breaks = seq(0,27, by = 3)) +
  theme_classic() +
  theme( legend.position = c(0.85, 0.85), 
         legend.title = element_blank())  #colours are modified custom in post 
library(aplot)
p2%>%
  insert_top(p1,height = 0.3)
image.png

最终的结果和论文中的图还是有区别的,比如散点图的配色以及图例的位置等等,他的代码里也添加了注释 colours are modified custom in post ,应该是指颜色后期有修改吧,然后如何将两个图放到一起也没有写

好了今天的内容就到这里了
欢迎大家关注我的公众号
小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

你可能感兴趣的:(跟着Nature Medicine学画图:R语言散点图组合频率分布图的简单小例子)