r - 在 ggplot2 的 aes() 中使用组参数
问题描述
我正在尝试在 ggplot2 的 aes() 中使用“组”参数,但我不确定为什么它不能像我目前拥有的那样工作。
我想要一个图像,以该图像使用“sex”的方式对我的“maskalthalf”变量进行分组(在此处找到)。
这就是我的图表目前的样子。
这是我到目前为止的代码。
ggplot(groups, aes(x = message, y = mean, group = factor(maskalthalf))) +
geom_bar(stat = "identity", width = 0.5, fill = "003900") +
geom_text(aes(label = round(mean, digits = 1), vjust = -2)) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
labs(title = "Evaluations of Personal and General Convincingness") +
ylab("Rating") +
xlab("Personal evaluation or general evaluation") +
ylim(0, 8)
这是我的目标的草图:
数据:
structure(list(maskalthalf = c("High", "High", "Low", "Low"),
message = c("General", "Personal", "General", "Personal"),
mean = c(4.79090909090909, 6.38181818181818, 4.69879518072289,
4.8433734939759), se = c(0.144452868727642, 0.104112130946133,
0.149182255019704, 0.180996951567937)), row.names = c(NA,
-4L), groups = structure(list(maskalthalf = c("High", "Low"),
.rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
解决方案
第一个示例中的图像使用构面按变量分组。所以你可以试试:
ggplot(df1, aes(x = message, y = mean)) +
geom_col(width = 0.5, fill = "003900") +
geom_text(aes(label = round(mean, digits = 1), vjust = -2)) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = .2, position = position_dodge(.9)) +
labs(title = "Evaluations of Personal and General Convincingness") +
ylab("Rating") +
xlab("Personal evaluation or general evaluation") +
ylim(0, 8) +
facet_wrap(~maskalthalf)
推荐阅读
- javascript - 在 ReactJS 中一次渲染更多组件
- python - 深度学习算法的平均输出
- java - 在 Java 中创建一个组合两个 ArrayList 的方法
- python-3.x - 枕头在图像上绘制文本 - 在迭代 Python 之间清除
- azure - Azure Synapse Studio - 无法创建 SQL 池
- amazon-web-services - AWS EC2 cloud9 环境 - 在环境外检索项目文件
- javascript - 使用表数据中可观察到的剔除值来应用 CSS 类
- r - R - 从数据框中的嵌套数据框中获取特定行的最有效方法
- python - 我错过了 spaCy 词形还原中的预处理功能吗?
- python - 如何让函数打印单个“\”(反斜杠)?