R语言怎么实现散点图组合频率分布图

发布时间:2021-11-22 16:00:33 作者:iii
来源:亿速云 阅读:596

本篇内容介绍了“R语言怎么实现散点图组合频率分布图”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

 首先是频率分布直方图

部分数据如下R语言怎么实现散点图组合频率分布图

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

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')
 
R语言怎么实现散点图组合频率分布图  
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"))

 
R语言怎么实现散点图组合频率分布图  
image.png
 接下来是散点图

散点图的部分数据如下

R语言怎么实现散点图组合频率分布图  
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 

 
R语言怎么实现散点图组合频率分布图  
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)
 
R语言怎么实现散点图组合频率分布图    

“R语言怎么实现散点图组合频率分布图”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 怎么用R语言绘制散点图
  2. R语言如何绘制频率直方图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

r语言

上一篇:怎么用R语言ggplot2散点图并添加拟合曲线和置信区间

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》