关于双分组(每组组分均大于3)柱状图的绘制(带散点,带mean+sd,带显著性)

ggplot2版本

library(ggpubr)
library(scales)
library(Rmisc)
library(readxl)
library(RColorBrewer)
library(tidyverse)

area <- read_excel("./area.xlsx")
area_sum <- summarySE(area, measurevar="Value", groupvars=c("Group","Type"))
str(area_sum)
 $ Group: Factor w/ 5 levels "WL+ND","WL+HFD",..: 1 1 1 2 2 2 4 4 4 5 ...
 $ Type : num  1 2 3 1 2 3 1 2 3 1 ...
 $ N    : num  6 6 6 6 6 6 6 6 6 6 ...
 $ Value: num  0.7534 0.2185 0.0281 0.5646 0.2823 ...
 $ sd   : num  0.0926 0.0888 0.0506 0.0438 0.0851 ...
 $ se   : num  0.0378 0.0362 0.0206 0.0179 0.0347 ...
 $ ci   : num  0.0972 0.0932 0.0531 0.046 0.0893 ...
ggplot(area_sum,aes(x=Type,y=Value,color=Group,fill=Group,shape=Group))+
  geom_bar(stat ="identity",width = 0.8,fill="white",size=1.1,position = position_dodge(0.8),show.legend = F)+#柱状图
  geom_errorbar(aes(ymin=Value, ymax=Value+sd), width=0.4,size=1.1,show.legend = F,position = position_dodge(0.8))+#误差棒
  geom_jitter(size=1.5,alpha=0.8,position = position_jitterdodge(jitter.width = 0.5),data = area,show.legend = T)+#散点,加载原始数据及
  annotate(geom = "text",
           x=c(0.9,1.1,2.9),
           y=c(0.68,0.65,0.3),
           label=c("**","#","**"),
           size=c(10,5,10))+#添加统计性,需另外计算
  scale_color_manual(values = c("#bdbdbd","#636363","#31a354","#3182bd"))+
  scale_fill_manual(values = c("#bdbdbd","#636363","#31a354","#3182bd"))+
  scale_shape_manual(values = c(21,22,24,25))+
  scale_y_continuous(expand = c(0,0),limits = c(0,1),labels = percent)+
  scale_x_continuous(breaks=c(1,2,3),labels=c("<100","100~200",">200"))+
  labs(y="Percent of islets",x="Diameter of islets (μm)")+
  theme_classic()+
  theme(axis.text=element_text(size=12,face = "bold",colour = "black"),
        axis.title=element_text(size=12,face = "bold",colour = "black"),
        axis.ticks.length=unit(.2, "cm"),
        axis.line = element_line(colour = "black", size = 1.1),
        axis.ticks = element_line(colour = "black", size = 1.1),
        # legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12,face = "bold",colour = "black"),
        strip.text = element_text(size=12,face = "bold",colour = "black"),
        plot.title = element_text(hjust = 0.5,size = 14,face = "bold",colour = "black"))+
  guides(fill = guide_legend(override.aes = list(size = 4)))
ggsave("islets-area_exclude.png",width = 10,height = 5)

ggpubr版本

ggbarplot(area, x="Type", y="Value", 
          add = c("mean_sd","jitter"),
          size = 1.1,plot_type='l',
          error.plot = "upper_errorbar",
          color = "Group",fill="white",
          add.params=list(color = "Group",shape = "Group",fill="Group",size=1.1),#无法单独为bar和dot赋予各自的属性,bar和dot的size合一出图太不美观了,而且bar的宽度也无法调整,难受
          position = position_dodge(0.7))+
  scale_color_manual(values = c("#bdbdbd","#636363","#31a354","#3182bd"))+
  scale_fill_manual(values = c("#bdbdbd","#636363","#31a354","#3182bd"))+
  scale_shape_manual(values = c(21,22,24,25))+
  scale_y_continuous(expand = c(0,0),limits = c(0,1),labels = percent)+
  scale_x_discrete(breaks=c(1,2,3),labels=c("<100","100~200",">200"))+#只能转换为离散型变量
  labs(y="Percent of islets",x="Diameter of islets (μm)")+
  theme_classic()+
  theme(axis.text=element_text(size=12,face = "bold",colour = "black"),
        axis.title=element_text(size=12,face = "bold",colour = "black"),
        axis.ticks.length=unit(.2, "cm"),
        axis.line = element_line(colour = "black", size = 1.1),
        axis.ticks = element_line(colour = "black", size = 1.1),
        legend.position = "none",
        legend.title = element_blank(),
        legend.text=element_text(size=12,face = "bold",colour = "black"),
        strip.text = element_text(size=12,face = "bold",colour = "black"),
        plot.title = element_text(hjust = 0.5,size = 14,face = "bold",colour = "black"))+
  #stat_compare_means(aes(group=Group),label = "p.signif",label.y =0.9,method = "anova",hide.ns = T)
  #此处显示的是全局p值,真正的p值还是需重新添加,添加方式参考https://github.com/const-ae/ggsignif或https://cran.r-project.org/web/packages/ggsignif/vignettes/intro.html
  geom_signif(y_position=c(0.9,0.7, 0.3), 
              xmin=c(0.75,0.92,2.75), 
              xmax=c(0.92,1.09,2.92),
              annotation=c("**","#","*"),
              size = 1,
              textsize = c(10,5,10,10,5,10,10,5,10),#3个数据,但只允许我提供1个或9个值,考虑到#号会比较大,建议缩小其对应的文本大小
              tip_length=0.01)

一路看下来,还是ggplot2顺眼,想怎么调怎么调

你可能感兴趣的:(关于双分组(每组组分均大于3)柱状图的绘制(带散点,带mean+sd,带显著性))