柱状图加一笔即可完成升华
写在前面
长期以来,柱状图由于展示信息较少:均值和标准差/方差/标准误。所以没少被人们所诟病,箱线图似乎在这方面完整碾压了柱状图,展示了整个数据的结构。但近期我在顶级期刊NATURE上发现了柱状图。但是却多加了一笔。下面让我们看看是什么个骚操作。
library(ggplot2)
使用内置数据
library(dplyr)
databar<- iris %>%
group_by(Species) %>%
summarise(mean(Petal.Width), sd(Petal.Width))
colnames(databar) = c("group","mean","sd")
head(databar)p1 = ggplot() +
geom_bar(data = databar,aes(x= group,y = mean,fill = group),stat = "identity", width = 0.4,position = "dodge",colour= "black", alpha=0.5) +
geom_errorbar(data = databar,aes(x= group,y = mean,ymin=mean -sd, ymax=mean +sd),colour="black",width=0.1,size = 1)+
theme_void()
p1
p2 = ggplot() +
geom_errorbar(data = databar,aes(x= group,y = mean,ymin=mean -sd, ymax=mean +sd),colour="black",width=0.1,size = 1)+
geom_boxplot(data=iris, aes(x=Species , y=Petal.Width ,fill = Species),, alpha=0.5) +
geom_jitter(data=iris, aes(x=Species , y=Petal.Width ,fill = Species),position=position_jitter(0.17), size=4,pch = 21,color = "black") +
theme_void()
p2
p3 = ggplot() +
geom_bar(data = databar,aes(x= group,y = mean,fill = group),stat = "identity", width = 0.4,position = "dodge",colour= "black", alpha=0.5) +
# geom_text(data=iris, aes(x=Species , y=Petal.Width ))+
geom_errorbar(data = databar,aes(x= group,y = mean,ymin=mean -sd, ymax=mean +sd),colour="black",width=0.1,size = 1)+
geom_jitter(data=iris, aes(x=Species , y=Petal.Width ,fill = Species),position=position_jitter(0.17), size=4,pch = 21,color = "black") +
theme_void()
p3
# install.packages('patchwork')
library('patchwork')
p1 + p2 + p3