美文网首页生信绘图R
纯纯的ggplot2画好看的柱状图,统计、分面

纯纯的ggplot2画好看的柱状图,统计、分面

作者: 欧阳松 | 来源:发表于2021-07-17 14:30 被阅读0次
    1626501985614.jpg

    如果你有一个表,有三列,一列数据,一列分组,一列组别

    bar <- read.csv("bar.csv")
    library(ggplot2) #画图
    library(ggsignif) ### 统计
    
    ggplot(bar,
           aes(x=Group,y=Value,color=Group,fill=Group))+
        geom_bar(stat="summary",fun=mean,position="dodge")+ #柱状图
        stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.5,position = position_dodge(0.9))+ ##'mean_sd' 自动计算均数+标准差,添加误差棒,当然也可以计算mean+se,mean_ci等,跟ggpubr一模一样,width可以设置误差棒的宽度,而0.9是误差棒的位置
        facet_grid(~Team,scales = 'free')+ #分面
        theme_minimal(base_size = 13)+ #主题和字体大小
        scale_color_manual(values = c('steelblue','firebrick'))+
        scale_fill_manual(values = c('steelblue','firebrick'))+
        geom_signif(comparisons = list(c("Control","Treat")),test = 't.test')+
        labs(x=NULL,y='Relative gene expression')
    
    image.png

    当然也可以在柱状图上添加点,这样的好处是可以看到原始数据

    ggplot(bar,
           aes(x=Group,y=Value,color=Group,fill=Group))+
        geom_bar(stat="summary",fun=mean,position="dodge")+
        stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.5,position = position_dodge(0.9))+
        facet_grid(~Team,scales = 'free')+
        theme_minimal(base_size = 13)+
        scale_color_manual(values = c('steelblue','firebrick'))+
        scale_fill_manual(values = c('steelblue','firebrick'))+
        geom_signif(comparisons = list(c("Control","Treat")),test = 't.test')+
        labs(x=NULL,y='Relative gene expression')+
        geom_dotplot(stackdir = "center", binaxis = "y", 
                     fill = "lightgray", 
                     dotsize = 0.9,position = position_dodge(0.9)) #position很重要
    
    image.png
    如果不想显示具体的P值,还可以自动标星号, geom_signif里面加一句map_signif_level=T
    ggplot(bar,
           aes(x=Group,y=Value,color=Group,fill=Group))+
        geom_bar(stat="summary",fun=mean,position="dodge")+
        stat_summary(fun.data = 'mean_sd', geom = "errorbar", width = 0.5,position = position_dodge(0.9))+
        facet_grid(~Team,scales = 'free')+
        theme_minimal(base_size = 13)+
        scale_color_manual(values = c('steelblue','firebrick'))+
        scale_fill_manual(values = c('steelblue','firebrick'))+
        geom_signif(comparisons = list(c("Control","Treat")),map_signif_level=T,test = 't.test')+
        labs(x=NULL,y='Relative gene expression')+
        geom_dotplot(stackdir = "center", binaxis = "y", 
                     fill = "lightgray", 
                     dotsize = 0.9,position = position_dodge(0.9))
    
    image.png

    当然最简单的是ggpubr,但是底层限制死了,很多细节不能DIY,比如误差棒的宽度

    library(ggpubr)
    ggbarplot(bar,'Group','Value',fill = 'Group',color = 'Group',
              facet.by = 'Team',scale='free',
              palette = c('steelblue','firebrick'),
              add = c('mean_sd','dotplot'),
              ggtheme = theme_minimal())+
        stat_compare_means(comparisons = list(c("Control","Treat")),label = "p.signif", method = 't.test')
    
    image.png

    相关文章

      网友评论

        本文标题:纯纯的ggplot2画好看的柱状图,统计、分面

        本文链接:https://www.haomeiwen.com/subject/anohpltx.html