首页 > 解决方案 > ggplot2 中的平均值

问题描述

我目前正在做一个项目,我想在图表中找到平均值。

这是我写的 ggplot2 的代码

ggplot(data = estates, aes(x=town, y=resale_price , fill=town)) +
  geom_bar(stat='identity',position = 'dodge') +
  ggtitle("Resale Price based on estate")+xlab("Town")+ylab("Resale price")+
  theme(plot.title = element_text(color = "red",size = 10,face = "bold.italic",hjust = 0.5),
        axis.title.x = element_text(color = "red",size = 8.5,face = "bold"),
        axis.title.y = element_text(color = "red",size = 8.5,face = "bold")
  )

我在哪里放置平均函数以便它显示在条形图上?

非常感谢

标签: r

解决方案


此处发布了类似的答案:

箱线图显示平均值

像这样得到你的手段:

mu <- aggregate(resale_price ~  town, estates, mean)

然后将文本添加到ggplot

+ geom_text()

推荐阅读