ggplot2分组柱图barplot添加误差线以及显著标记

分组barplot添加均值标准差误差线显著标记

library(ggplot2)
library(ggpubr)
mydata1 <- data.frame(fenzu=rep(c(1,2),each=15),shijain=rep(c("入院","7天","14天"),each=5),value=seq(1,30,1))
mydata1$fenzu <- as.factor(mydata1$fenzu)
mydata1$shijain <- as.factor(mydata1$shijain)
mydata1$shijain <- factor(mydata1$shijain,levels = unique(mydata1$shijain))

p<- ggplot(mydata1, aes(x = shijain, y = value, fill = fenzu)) +  #下面fun后边跟的median或者mean一定要加引号!!否则会报错
  geom_bar(stat = "summary", fun ="mean", position = position_dodge(),alpha=0.7) +
  stat_summary(fun.data = 'mean_sd', geom = "errorbar", colour = "black",
              width = 0.15,position = position_dodge( .9))
#p#注意调透明度的位置
q <- p + stat_compare_means(method = "wilcox.test",method.args = list(alternative = "two.sided"))
q <- q+scale_fill_brewer(palette = 'Accent')+ theme(text = element_text(family = "wqy-microhei"))

#q
#p+stat_compare_means(aes(label =..p.signif..), method = "wilcox.test", method.args = list(alternative = "two.sided"))

q+geom_segment(x = 0.75, y = 8.817302 +3, xend =0.75 , yend =  32.302470-7  )+
  geom_segment(x = 0.75, y = 32.302470-7 ,xend = 1.23, yend = 32.302470-7) +
  geom_segment(x = 1.23, y = 32.302470-10 ,xend = 1.23, yend = 32.302470-7) +
  annotate("text", x = 1, y= 32.302470-6, label = "***")+  ggtitle("评分柱状图")+labs (y="均值")+labs (x="时间")+
  theme(plot.title = element_text(hjust = 0.5,size = 15))

在这里插入图片描述

p+stat_compare_means(aes(label =..p.signif..), method = "wilcox.test", method.args = list(alternative = "two.sided"))

在这里插入图片描述

没有原始数据只有均值标准差时画柱图加误差线

#---没有原始数据时画柱图带标准差
data <- data.frame(zubie=c("m","m","m","b","b","b"),zt=c("入院","7天","14天","入院","7天","14天"),mean=c(0.3,0.4,0.5,0.6,0.7,0.8),sd=c(0.1,0.3,0.2,0.6,0.4,0.5))
data$zubie <- factor(data$zubie,levels = unique(data$zubie))
ggplot(data = data,aes(x =as.factor(zubie), y = mean, fill = zt))+
  geom_bar(stat = "identity",position = "dodge",)+
  geom_errorbar(aes(ymax = mean+sd, ymin = mean-sd),
                position = position_dodge(0.9), width = 0.15)+
  scale_fill_brewer(palette = 'Accent')+
  ggtitle("评分柱状图")+labs (y="均值")+labs (x="时间")+
  theme(plot.title = element_text(hjust = 0.5,size = 10))

在这里插入图片描述

关于bar堆叠的问题,一些参数解释

参考:
geom_bar参数.

geom_bar(mapping = NULL, data = NULL, stat = “count”, width=0.9, position=“stack”)

  • stat:设置统计方法,有效值是count(默认值) 和 identity,其中,count表示条形的高度是变量的数量,identity表示条形的高度是变量的值;
  • position:位置调整,有效值是stack、dodge和fill,默认值是stack(堆叠),是指两个条形图堆叠摆放,dodge是指两个条形图并行摆放,fill是指按照比例来堆叠条形图,每个条形图的高度都相等,但是高度表示的数量是不尽相同的。
  • width:条形图的宽度,是个比值,默认值是0.9
  • color:条形图的线条颜色
  • fill:条形图的填充色
position_stack(vjust = 1, reverse = FALSE)
position_dodge(width = NULL)
position_fill(vjust = 1, reverse = FALSE)
data(Arthritis)
library(vcd)
library(ggplot2)
data("Arthritis")
ggplot(data=ToothGrowth, mapping=aes(x=dose))+
  geom_bar(stat="count")

#-------------
mytable <- with(Arthritis,table(Improved))
df <- as.data.frame(mytable)

ggplot(data=df, mapping=aes(x=Improved,y=Freq))+
  geom_bar(stat="identity")

#--------------
ggplot(data=Arthritis,aes(x=Improved))+
  geom_bar(stat="count",width=0.5, color='red',fill='steelblue')

#--------------
ggplot(data=Arthritis, mapping=aes(x=Improved))+
  geom_bar(stat="count",width=0.5, color='red',fill='steelblue')+
  geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
  theme_minimal()  #aes(label=..count..)表示显示变量值


#---------------
mytable <- with(Arthritis,table(Improved))
df <- as.data.frame(mytable)

ggplot(data=df, mapping=aes(x=Improved,y=Freq))+
  geom_bar(stat="identity",width=0.5, color='red',fill='steelblue')+
  geom_text(aes(label=Freq), vjust=1.6, color="white", size=3.5)+
  theme_minimal()
#----------------------

    #color 和 fill 可更改的颜色参数,一般柱状图,fill是柱子的填充颜色,
    #这时就使用scale_fill系列函数来更改颜色。点图和线使用color分组,
    #则使用scale_color_系列函数来更改颜色。

ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Improved))+
  geom_bar(stat="count",width=0.5)+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  #scale_color_manual(values=c("56B4E9", "#56B4E9", "#56B4E9"))+
  geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
  theme_minimal()

#-----------------
    #图例
p <- ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Improved))+
  geom_bar(stat="count",width=0.5)+
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
  theme_minimal()

p + theme(legend.position="top")
p + theme(legend.position="bottom")
# Remove legend
p + theme(legend.position="none")

#--------------
    #修改条形图顺序
p <- ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Improved))+
  geom_bar(stat="count",width=0.5)+
  scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
  geom_text(stat='count',aes(label=..count..), vjust=1.6, color="white", size=3.5)+
  theme_minimal()

p + scale_x_discrete(limits=c("Marked","Some", "None"))


#-----------------
    #堆叠
ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Sex))+
  geom_bar(stat="count",width=0.5,position='stack')+
  scale_fill_manual(values=c('#999999','#E69F00'))+
  geom_text(stat='count',aes(label=..count..), color="white", size=3.5,position=position_stack(0.5))+
  theme_minimal()
    
    #并行
y_max <- max(aggregate(ID~Improved+Sex,data=Arthritis,length)$ID)
#aggregate(Arthritis$ID,list(Arthritis$Improved,Arthritis$Sex),length)

p <- ggplot(data=Arthritis, mapping=aes(x=Improved,fill=Sex))+
  geom_bar(stat="count",width=0.5,position='dodge')+
  scale_fill_manual(values=c('#999999','#E69F00'))+
  ylim(0,y_max+5)+
  geom_text(stat='count',aes(label=..count..), color="black", size=3.5,position=position_dodge(0.5),vjust=-0.5)+
  theme_minimal()



#添加文本的函数用的时候可以再了解
#annotate()  位置和文本字体大小角度可以随意设置                                                   
p+annotate("text",x=3,y=30,label="冲鸭~~",size=11,angle=30,alpha=0.2)#+
  


#annotate("text",x=7,y=2.2,label="跟着菜鸟一起学R语言",size=8,angle=-15,alpha=0.9,colour="red",family="JP3")+
 # annotate("text",x=7,y=4,label="跟着菜鸟一起学R语言",size=6,angle=-30,alpha=0.5,colour="blue",family="JP4")+
#  annotate("text",x=5,y=4,label="跟着菜鸟一起学R语言",size=8,angle=-10,alpha=0.7,colour="green",family="JP1")+
 # annotate("text",x=5,y=3,label="跟着菜鸟一起学R语言",size=6,angle=-30,alpha=0.5,colour="blue",family="JP5")


在这里插入图片描述

在这里插入图片描述