前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言ggplot2每周一图活动第五周:箱线图、小提琴图

R语言ggplot2每周一图活动第五周:箱线图、小提琴图

作者头像
用户7010445
发布2022-05-23 15:57:37
6290
发布2022-05-23 15:57:37
举报
文章被收录于专栏:小明的数据分析笔记本

在之前提到的tidytuesday里没有找到关于箱线图和小提琴图比较好的实例,这周的ggplot2每周一图活动用之前推文中的内容作为实例进行讲解,之前的推文是跟着Nature学作图:R语言ggplot2箱线图、小提琴图、抖动散点图

作图代码

代码语言:javascript
复制
dfToPlot<-read.csv("dfToPlot.csv")
head(dfToPlot)
dfToPlot$RELATIONSHIP.0 <- factor(dfToPlot$RELATIONSHIP.0,
                                  levels=c("RND.PAIR","PARTNERS","PARENT_CHILD","SIBLINGS"))
library(ggplot2)
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
                         y=BC_Spec,
                         color=RELATIONSHIP.0))+
  geom_boxplot() -> p1

p1

ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
                         y=BC_Spec,
                         color=RELATIONSHIP.0))+
  geom_jitter() -> p2
p2

ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
                         y=BC_Spec,
                         color=RELATIONSHIP.0))+
  geom_violin() -> p3
p3

library(patchwork)
p1+p2+p3+
  plot_layout(guides = "collect")


cbPalette <- c("#E69F00", "#CC79A7", "#56B4E9", "#009E73", "#CC79A7", "#F0E442", "#999999","#0072B2","#D55E00")
ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
                         y=BC_Spec,
                         color=RELATIONSHIP.0))+
  geom_jitter(alpha=0.2,
              position=position_jitterdodge(jitter.width = 0.35, 
                                            jitter.height = 0, 
                                            dodge.width = 0.8))+
  geom_boxplot(alpha=0.2,width=0.45,
               position=position_dodge(width=0.8),
               size=0.75,outlier.colour = NA)+
  geom_violin(alpha=0.2,width=0.9,
              position=position_dodge(width=0.8),
              size=0.75)+
  scale_color_manual(values = cbPalette)+
  theme_classic() +
  theme(legend.position="none") + 
  theme(text = element_text(size=16)) + 
  #ylim(0.0,1.3)+
  ylab("Bray-Curtis distance of Species")

ggplot(data=dfToPlot,aes(x=RELATIONSHIP.0,
                         y=BC_Spec,
                         color=RELATIONSHIP.0))+
  geom_jitter(alpha=0.2,
              position=position_jitterdodge(jitter.width = 0.35, 
                                            jitter.height = 0, 
                                            dodge.width = 0.8))+
  geom_boxplot(alpha=0.2,width=0.45,
               position=position_dodge(width=0.8),
               size=0.75,outlier.colour = NA)+
  geom_violin(alpha=0.2,width=0.9,
              position=position_dodge(width=0.8),
              size=0.75)+
  scale_color_manual(values = cbPalette)+
  theme_classic() +
  theme(legend.position="none") + 
  theme(text = element_text(size=16)) + 
  #ylim(0.0,1.3)+
  ylab("Bray-Curtis distance of Species")+
  #scale_x_discrete(labels=c("A","B","C","D"))+
  annotate("segment", x = 1-0.01, y = 1, xend = 2.01,lineend = "round", 
           yend = 1,size=1,colour="black",arrow = arrow(length = unit(0.02, "npc")))+
  annotate("segment", x = 2.01, y = 1, xend = 0.99,lineend = "round", 
           yend = 1,size=1,colour="black",arrow = arrow(length = unit(0.02, "npc")))+
  annotate("text", x=1.5,y=1.01, 
           label=expression("**"~"FDR"~2.41%*%10^-10),vjust=0) -> p4
p4

p4+p4+scale_color_manual(values = cbPalette[5:8])

最终结果

image.png

image.png

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 作图代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档