ggplot 条形图添加误差棒

一般在实验中样本存在多组值的时候,条形图通常都会加上误差棒,这里的画图脚本留作参考和备忘。

测试数据

sample	A	B	C	D	E	F
yzx	587.38	184.66	236.97	5.28	0.44	1.76
yzx	123.52	189.88	145.47	1.53	1.53	0
yzx	801.62	77.17	49.09	67.68	42.42	88.89
zjt	413.95	352.2	294.32	219.57	195.86	170
zjt	438.39	356.72	288.38	198.43	194.22	167.41
zjt	411.13	360.92	305.91	204.64	189.51	181.03
gwj	8.23	23.53	3.53	3.92	1.18	2.74
gwj	336.1	49.81	6.05	2.79	1.4	1.86
gwj	8.58	105.35	31.22	3.9	3.51	2.34
sym	3.68	0	2.37	2.37	2.37	0.26
sym	6.69	1.74	1.45	1.74	2.91	0.29
sym	3.13	3.65	2.35	1.83	1.04	0.78
zly	655.57	320.76	289.21	240.01	170.01	149.21
zly	662.16	329.72	274.31	219.26	157.09	164.9
zly	683.68	362.05	285.01	235.2	161.82	157.45
tzh	439.66	404.52	287.44	213.31	187.71	171.3
tzh	422.59	392.34	304.45	192.4	190.72	162.03
tzh	400.64	386.18	297.78	193.79	194.04	168.3
cfc	345.53	370.73	270.4	184.36	176.68	157.79
cfc	365.29	350.5	261.2	179.59	178.41	154.95
cfc	387.2	380.67	246.42	174.97	173.24	152.88
cdc	373.42	396.97	290.43	168.32	177.01	146.94
cdc	374.2	353.26	276.48	174.16	195.27	165.48
cdc	362.86	373.34	265.13	190.14	184.24	155.74
dfk	0	1.51	1	0	0	0.5
dfk	1.16	0	0	0	0	0
dfk	0.93	0	2.33	0	0.47	0

标准误公式

sem <- sd(x)/sqrt(length(x))

画图代码

library(ggplot2)
library(reshape2)

df<-read.table("data.txt",header = T)
df2<-melt(df)

ggplot(df2,aes(x=variable,y=value))+
  labs(y="value",x="condtion")+
  stat_summary(geom = 'bar',fun = 'mean',cex=1,width=.8,fill='purple')+
  stat_summary(geom = 'errorbar', width=.2,cex=1,color='tomato',
               fun.min = function(x){return(mean(x)-sd(x)/sqrt(length(x)))},
               fun.max = function(x){return(mean(x)+sd(x)/sqrt(length(x)))}
               )+
  facet_wrap(.~sample,nrow = 3,scales="free")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 90,vjust = 0.5,hjust = 1,size=9))

结果

image

posted @   天使不设防  阅读(334)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示