mean - 具有分面网格的 geom_bar 的平均函数
问题描述
我想用 2 个分面网格和属于每个分面组的值的平均值来执行 geom_bar 可视化。酒吧出来的好,但我似乎无法让颜色和文字出来。我的代码如下(也尝试使用 stat_summary 和 geom_text ):
index_chart <- index_retrenchment %>%
ggplot(aes(y = change,
x = industry)) +
geom_bar(aes(fill = change), stat = "summary", fun = mean)+
scale_fill_gradient(low = "red", high = "green")+
#stat_summary(aes(label = change), fun = mean, geom = "text", size = 3)+
#geom_text(label = index_retrenchment$change, size = 3)+
labs(title = "Comparison of Retrenchment Rate during Various Global Events",
subtitle = "Retrenchment Rate vs Average Retrenchment Rate (Year 1998 to 2020)",
y = "Change in Retrenchment Rate (Percentage)",
x = "Industry",
caption = "Source: https://data.gov.sg/")+
theme_economist_white()+
theme(legend.title = element_blank())+
theme(axis.text.x = element_text(angle = 90))+
facet_grid(STI~NASDAQ)
我的数据看起来像(对不起长链):
STI <- c(“低”、“低”、“高”、“高”、“高”、“低”、“高”、“高”、“高”、“高”、“低”、“低”,“高”,“低”,“低”,“低”)纳斯达克 <- c(“高”,“高”,“低”,“低”,“高”,“低”,“低” "、"低"、"低"、"低"、"高"、"高"、"高"、"低"、"高"、"高") 年 <- c(1998, 1998, 1998, 1998 , 1999, 1999, 1999, 1999, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001) 工业 <- c("manufacturing", "construction", "services", "others", "manufacturing", “建造”,“服务”、“其他”、“制造”、“建筑”、“服务”、“其他”、“制造”、“建筑”、“服务”、“其他”)更改 <- c(134, 30, - 45, 46, -76, 98, 78, -10, 120, -90, -54, 30, 21, 11, 17, -87)
数据 <- tibble(STI、纳斯达克、年份、行业、变化)
从我的代码来看,颜色和比例似乎不适用于其他 3 组。颜色出现的组只有一组变量,我相信这就是它起作用的原因。但我不确定如何从这里开始。
所以基本上我在比较 4 个不同组的平均数据(即当 STI 低和 NASDAQ 低、STI 高和 NASDAQ 高等时)
总之,我希望能够应用颜色并将平均值文本添加到所有条形图。如果解释不够清楚或测试数据不足,请提前道歉。
解决方案
推荐阅读
- python - 删除scrapy xpath中的特殊字符
- css - 悬停在其父li之外时如何保持下拉菜单可见
- python - 了解 Python:UnboundLocalError
- python - SQLite 和 tableWidget
- github-actions - 如何在 GitHub Actions 配置 yaml 中集中(干燥)字符串?
- python - 如何在 Python 中附加分页响应中的数据集?
- sql - 是否有替代子选择上的多个左连接的替代方法?
- laravel - 如何访问从 laravel 6.x 到 vuejs 的特定变量(我尝试动态实现鼠标悬停)
- html - 无法将内容添加到下一页 css html
- javascript - 如何使按钮单击只执行一次代码