ggplot2绘制独特的分面图

本节来介绍如何使用ggplot2自定义绘制分面并添加统计信息,以及单分面数据注释

加载R包

library(tidyverse)
library(ggsignif)
library(ggsci)

绘制并排箱线图

通常geom_boxplot函数绘制的箱线图都是不带误差线的,在此我们通过stat_boxplot添加误差线

iris %>% pivot_longer(-Species) %>% 
  mutate(name=as.factor(name)) %>% 
  ggplot(aes(name,value,fill=Species))+
  stat_boxplot(geom="errorbar",
               position=position_dodge(width=0.8),width=0.2)+
  geom_boxplot(position=position_dodge(width =0.8))
image.png

统计检验

p <- iris %>% pivot_longer(-Species) %>% 
  mutate(name=as.factor(name)) %>% 
  ggplot(aes(Species,value,fill=Species))+
  stat_boxplot(geom="errorbar",
               position=position_dodge(width=0.8),width=0.2)+
  geom_boxplot(position=position_dodge(width =0.8))+
  geom_signif(comparisons = list(c("setosa", "versicolor"),
                                 c("virginica","versicolor"),
                                 c("setosa","virginica")),
              map_signif_level=T,vjust=0.5,color="black",
              textsize=5,test=wilcox.test,step_increase=0.1)+
  facet_wrap(.~name,nrow=1)+
  scale_fill_jco()+
  theme(legend.title=element_blank())+
  labs(x=NULL,y=NULL)+
  theme_classic()+
  theme(strip.background = element_rect(fill="grey80",color="black"),
        strip.text.x = element_text(size=10,color="black"),
        axis.text.y=element_text(size=10,color="black"),
        axis.ticks.x = element_blank(),
        axis.text.x=element_blank(),
        panel.spacing = unit(0,"lines"),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        legend.text = element_text(color="black",size=10),
        legend.title=element_blank(),
        legend.spacing.x=unit(0.1,'cm'),
        legend.key=element_blank(),
        legend.key.width=unit(0.6,'cm'),
        legend.key.height=unit(0.6,'cm'),
        plot.margin=unit(c(0.3,0.3,0.3,0.3),units=,"cm"))

单分面注释

有时可能只需要对单分面进行注释,此时可通过自定义数据集来给单个分面添加内容

data.segm<-data.frame(x=0.5,y=5,xend=4,yend=5,name="Sepal.Width")

ann_text <- data.frame(Species="versicolor",value=5.5,lab = "Text",
                       name= factor("Sepal.Width",
                                    levels = c("Petal.Length","Petal.Width",
                                               "Sepal.Length","Sepal.Width")))
p + geom_text(data = ann_text,label = "Sepal.Width")+
  geom_segment(data=data.segm,color="red",
               aes(x=x,y=y,yend=yend,xend=xend),inherit.aes=FALSE)

喜欢的小伙伴欢迎关注我的公众号

R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 写在前面 ggplot2 是一个功能强大且灵活的R包 ,由Hadley Wickham 编写,其用于生成优雅的图...
    Boer223阅读 28,454评论 0 67
  • 作者:严涛浙江大学作物遗传育种在读研究生(生物信息学方向)伪码农,R语言爱好者,爱开源 ggplot2学习笔记之图...
    Dylan的迪阅读 7,662评论 0 6
  • 主要从如何看图、用图与作图三个方面来对箱线图进行理解和总结。 1、看图 如图所示,箱线图是将一组数据按照大小顺序排...
    dowaves阅读 77,281评论 17 109
  • 1、两个变量:x,y皆连续 使用数据集mtcars mtcars数据集包含从1974年《美国汽车趋势》杂志中提取的...
    了无_数据科学阅读 6,447评论 0 2
  • 参考自https://www.cnblogs.com/ljhdo/p/4921588.html 作者:悦光阴 箱线...
    onlyme_862a阅读 14,005评论 0 13