美文网首页R作图
R语言条形图(误差线+统计显著性)

R语言条形图(误差线+统计显著性)

作者: 研究僧小蓝哥 | 来源:发表于2019-10-23 23:43 被阅读0次

条形图是常见的统计图形,实现的软件很多,比较钟情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')

最后的图:


by 小蓝哥

相关文章

网友评论

    本文标题:R语言条形图(误差线+统计显著性)

    本文链接:https://www.haomeiwen.com/subject/rcvlvctx.html