【R画图学习9.5】加入误差线的柱状图

还是我们前两次的测试数据:

library(ggplot2)

data <- read.table("week.data.txt",header=T,sep="\t")

head(data)

ggplot(data, aes(x = day, y = frequency, fill = week_n)) +

  geom_bar(stat = "identity", color = "white",lwd = 1, show.legend = FALSE,width = 0.6)+

  scale_x_continuous(breaks=seq(1,21,1),labels=data$week)+

  theme(text = element_text(size = 15))+

  geom_errorbar(aes(ymin=frequency-sad, ymax=frequency+sad),width=0.2,position=position_dodge(0.6),size=0.8)

主要通过geom_errorbar来添加误差线。比如我们先测试的是已知的误差列sad。

我们也可以自己计算不同week之间的均值和方差,然后画图。

data_new <- data %>%

  group_by(week_n) %>%

  mutate(mean = mean(frequency), group_max = max(frequency),var=mad(frequency) ) %>%

  arrange(desc(mean))

data_new2 <- unique(data_new[,c(2,8,9,10)])

my_comparisons <- list( c("week1", "week2"), c("week1", "week3"), c("week2", "week3") )

ggplot(data_new2, aes(x = week_n, y = mean, fill = week_n)) +

  geom_bar(stat = "identity", color = "white",lwd = 1, show.legend = F,width = 0.6)+

  theme(text = element_text(size = 15))+

  geom_errorbar(aes(ymin=mean-var, ymax=mean+var),width=0.2,position=position_dodge(0.6),size=0.8)+

  stat_compare_means(comparisons = my_comparisons)+

  stat_compare_means(label.y = 60,label.x = 1.5) 

图中,我们通过geom_errorbar加入了误差bar,并且运用了前面的技巧,通过stat_compare_means做了两两之间的统计显著性检验。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容