首页 > 解决方案 > stat_summary 条形图标签

问题描述

有人可以帮我理解如何在 ggplot2 中使用 stat_summary 将平均标签稍微放在条形图上方吗?这是我的代码:

#Create data frame
industry = c(1,1,1,1,1,2,2,2,2,2) 
choice = c(1,2,3,2,1,4,5,3,5,5) 
df =data.frame(industry,choice)

#Define data to plot
line <- ggplot(df, aes(industry, choice)) 

#Plot data, bar chart
test <- line + 
  theme_classic() + 
  stat_summary(fun.y = mean, geom = "bar", width=0.4, aes(group = 1)) +    
  labs(x = "product", y = "firm size preference") +  
  scale_x_discrete(limits=c("pizza","headphones")) + 
  theme(text = element_text(size=12)) 
test + 
  ggtitle(label =  "FIGURE 1", subtitle = "FIRM SIZE PREFERENCE") +   
  theme(
    plot.title = element_text(hjust = 0.5, size = 12), 
    plot.subtitle = element_text(hjust = 0.5, size = 10)
  )

标签: rggplot2

解决方案


推荐阅读