r - 如何在 R 中创建具有次要分组 x 轴的条形图?
问题描述
我希望在 R 中使用 ggplot 重新创建以下条形图,但到目前为止我没有运气(请忽略白线,我不得不删除确切的数据):
我的数据安排如下,我认为这是合适的:
figure_1 <- tribble(
~"ResponseOption", ~"StimuliFormat", ~"rfg", ~"emmean", ~"SE",
"RF_Ratings", "Picture", "Recollection", 2, 0.03,
"RFBG", "Picture", "Recollection", 1, 0.03,
"RFG", "Picture", "Recollection", 7, 0.03,
"RF_Ratings", "Word", "Recollection", 04, 0.03,
"RFBG", "Word", "Recollection", 3, 0.03,
"RFG", "Word", "Recollection", 5, 0.03,
"RF_Ratings", "Picture", "Familiarity", 2, 0.03,
"RFBG", "Picture", "Familiarity", 1, 0.03,
"RFG", "Picture", "Familiarity", 7, 0.03,
"RF_Ratings", "Word", "Familiarity", 04, 0.03,
"RFBG", "Word", "Familiarity", 3, 0.03,
"RFG", "Word", "Familiarity", 5, 0.03,
"RF_Ratings", "Picture", "Guessing", 2, 0.03,
"RFBG", "Picture", "Guessing", 1, 0.03,
"RFG", "Picture", "Guessing", 7, 0.03,
"RF_Ratings", "Word", "Guessing", 04, 0.03,
"RFBG", "Word", "Guessing", 3, 0.03,
"RFG", "Word", "Guessing", 5, 0.03)
但我不知道如何在 x 轴上有 2 个分组变量(文字/图片 + RFG/RFBG/RFRatings)。我可以制作 3 个单独的条形图并以某种方式(?)将它们连接在一起,但我正在寻找一个更优雅的解决方案,我可以将两个 x 轴分组变量输入到 ggplot 中。
任何帮助或指导将不胜感激!
解决方案
使用facet_wrap
您最终可以通过使用参数传递底部的构面标签并使用以下方法strip.position
将它们添加到绘图区域之外来获得类似的绘图strip.placement = "outside"
:
ggplot(figure_1, aes(x = StimuliFormat, y = emmean, fill = rfg))+
geom_col(position = position_dodge())+
geom_errorbar(aes(ymin = emmean-SE, ymax = emmean+SE), width =0.2, position = position_dodge(0.9))+
facet_wrap(~ResponseOption, strip.position = "bottom")+
theme_classic()+
theme(strip.placement = "outside")+
labs(x = "", y = "Proportion of hits")
它回答了你的问题吗?
推荐阅读
- html - 显示两个跨度元素,一个在另一个之下
- amazon-ec2 - 使用用户数据、云初始化指令和元数据的合适场景是什么
- python - 试图理解 numpy 数组负索引
- r - R:循环在 R 中创建新的数据帧
- javascript - 如何在文本之间插入 img?反应原生
- javascript - 我想拖动一个定义为数据属性的元素,我的问题是如何在这段代码中解决它:
- python - 图像搜索 Python 始终运行
- python-3.x - 如何从图像中去除非文本或背面打印噪音?
- php - Square up php create card using php sdk api-card nonce error
- reactjs - 在 React JS 中设置背景图片