r - r中每个类别的条形图平均值
问题描述
此处是 R 新手,但无法找到这个可能简单问题的答案。我这里有一些数据:
我只想绘制民主党人、共和党人和独立人士的平均信任水平,如下所示:
...使用ggplot。
我尝试了以下代码(Q4 = 政党从属列)...
hypothesis_1 <- ggplot(data = data.df, aes(x = Q4, y = avgTRUST))
graph_4 <- hypothesis_1 + geom_col(stat = 'identity', fill = "blue") + ggtitle("Trust in Government Institutions (by Political Party)") + theme_minimal() + theme(axis.title.x=element_blank()) + labs(y = "Trust Levels") + scale_y_continuous(breaks = seq(0,30,1))
解决方案
这可以工作,但在缺乏可重复数据的情况下无法测试:
library(tidyverse)
#Code
data.df %>%
group_by(Q4) %>%
summarise(avgTRUST=mean(avgTRUST,na.rm = T)) %>%
ggplot(aes(x = Q4, y = avgTRUST))+
geom_col(stat = 'identity', fill = "blue") +
ggtitle("Trust in Government Institutions (by Political Party)") +
theme_minimal() +
theme(axis.title.x=element_blank()) +
labs(y = "Trust Levels")
推荐阅读
- karate - 空手道匹配每个响应断言未能识别响应中缺少的键
- javascript - Mongodb聚合以对数组中最常见的项目进行排序?
- python - 使用 Selenium 获取响应代码的最佳方法?
- ios - iOS:Metal、CoreImage 和 GL 着色器
- list - F# - 我怎样才能将一个整数划分为(大部分相等)比这更好的“桶”?
- java - 使用 Quarkus 将消息从 Kafka Streams 发送到 WebSocket
- c# - c#,多个异步任务执行
- fortran - HDF5 抛出错误,除非我内联子程序
- reactjs - 如何在 react-select 中使用 isMulti 处理多个选项
- spring-boot - Spring Kafka在消费者重试完成后产生消息