条形图是常见的统计图形,实现的软件很多,比较钟情R,话不多说,直接上代码。
# 清空当前变量
rm(list = ls())
###################################
############构建绘图数据###########
###################################
cat = c('a','b','c','d')
mean = c(13,15,12,14)
sd = c(1,1.5,1,2)
sigf = c('*','n.s.','**','*')
data = data.frame(cat,mean,sd,sigf)
###################################
############ggplot绘图#############
###################################
library(ggplot2)
ggplot(data = data, mapping = aes(x = cat, y = mean,fill = cat))+
geom_bar(stat = 'identity',width = 0.6)+
geom_errorbar(mapping = aes(x = cat, ymin = mean - sd, ymax = mean + sd),width = 0.4)+
geom_text(mapping = aes(x = cat, y = mean + 1.5*sd),label = sigf,size = 5)+
geom_hline(aes(yintercept = 18),col = 'white')+
scale_y_continuous(expand = c(0,0))+
theme_classic()+
theme(legend.position = 'none')
最后的图:

网友评论