r - 我想要一个并排的 barplor (R)
问题描述
我在 data.frame 中有两列。假设一个称为“之前”,另一个称为“之后”。这 2 个因素中的每一个都有 5 个级别(非常满意、满意、不知何故、不满意、非常不满意)。
这是数据:
dat <- structure(list(before = structure(c(2L, 5L, 5L, 1L, 3L, 3L), .Label = c("very satisfied",
"satisfied", "somehow", "dissatisfied", "very dissatisfied"), class = "factor"),
after = structure(c(3L, 3L, 5L, 2L, 2L, 2L), .Label = c("very satisfied",
"satisfied", "somehow", "dissatisfied", "very dissatisfied"
), class = "factor")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
如何构建一个并排的情节,以便对于这 5 个级别中的每一个,有 2 个条(之前和之后)并排表示?
解决方案
透视数据后,您可以使用position = "dodge"
并排获取您的组。
dat %>%
pivot_longer(everything(), names_to = "period", values_to = "score") %>%
ggplot(aes(x = score, group = period, fill = period)) +
geom_bar(position = "dodge")
推荐阅读
- java - 带有用于字符串列表的弹簧引导规范的 IN 子句
- android - 如何在具有不同 SIGN 凭据的终端中构建 apk
- visual-studio-app-center - 如何在 App Center 中为 Android 或 iOS 构建指定文件名
- postgresql - SpringbootTest + TestContainers:测试污染数据库后如何刷新数据库
- c# - 输入模拟器虚拟键代码 RETURN 充当空格键
- swift4 - 将 responseJSON 解析为 ObjectMapper
- r - 如何使用 dplyr 计算列范围内连续两列集之间的差异
- c# - 如何将excel文件从角度导入asp.core,而不是读取数据并将其推送到列表中
- xml - XSLT 无法用破折号重命名标签
- google-apps-script - 如何以编程方式复制 Google Apps 脚本中文件绑定脚本的脚本权限?