還是我們前兩次的測試數(shù)據(jù):
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婉弹。
我們也可以自己計(jì)算不同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,并且運(yùn)用了前面的技巧史翘,通過stat_compare_means做了兩兩之間的統(tǒng)計(jì)顯著性檢驗(yàn)备籽。