在 R 中并排创建多组直方图

Create multigroup histograms side by side in R

我正在尝试在 R 中复制 these histograms 的一项研究。

我有一个来自这项研究的庞大数据集,所以我认为我无法将其粘贴在这里,但这里有一个简短的版本:

menutype menuselection belieflearn learned
5              1           0          0
11             1           1          0
2              3           0          0
2              3           0          0
2              1           0          0
2              1           0          0
10             1           0          0
12             3           0          0
8              3           0          1
12             3           0          0

思路是这样的:首先,我select只有变量所在的变量“menuselection == 3”。然后,对于这些变量,对于分别对应于 "GUILT"、"SSB0"... 的每个 menutype 值(在 1:7 范围内),我计算频率取决于玩家是否期望选项 1(所以如果 belieflearn == 1)以及玩家选择选项 1 时的频率(所以如果 learned == 1)。

我认为 factor() 需要在这里使用,但我不太明白如何使用。我关注了 并且尝试了这个:

df2 <- data.frame(
  menutype =  factor(df$menutype, labels = c("GUILT", "SSB0", "SSB1", "FLEX0", "FLEX1", "STD", "FLEX01", "test1","test2", "test3", "test4", "test5" )),
  Belief = factor(df$belieflearn, labels= c("Believe not learn", "Believe Learn")),
  Choice = factor(df$learned, labels= c("Not learn", "Learn"))
)


df3 <- df2 %>%
  count(Belief, menutype) %>%
  group_by(Belief) %>% 
  mutate(prop = n / sum(n))


ggplot(data = df3, aes(menutype, prop , fill = Belief)) +  
  geom_bar(stat = "identity", position = "dodge")

哪个有效,但我想排除 menutype>7 的值(我放置 test1、test2 以使 factor() 正常工作,但最理想的是,我想摆脱它们)。我试过 exclude() 但没有成功。

我也没有为菜单指定selection == 3。也许循环应该做到这一点?

我得到的是 this 图表。 显然,我做错了什么,因为我应该为每个菜单类型设置两个栏,比例为 Belieflearned

此外,我对 R(和 Whosebug)还很陌生,所以如果有什么我应该添加到这个线程的,请告诉我!

感谢您的帮助。

编辑:我在原来的学习中找到了用于生成图形的Stata代码,所以这里是:

  graph bar (mean) belieflearn learned if menuselection==3, over(menutype, relabel(1 "{it:{stSerif:GUILT}}" 2 "{it:{stSerif:SSB_{subscript:0}}}" 3 "{it:{stSerif:SSB_{subscript:1}}}" 4 "{it:{stSerif:FLEX_{subscript:0}}}" 5 "{it:{stSerif:FLEX_{subscript:1}}}" 6 "{it:{stSerif:STD_{subscript:0}}}" 7 "{it:{stSerif:FLEX_{subscript:0v1}}}" ))  ///
ytitle("fraction of subjects") yvaroptions(relabel(1 "expected Option 1 (reading)" 2 "chose Option 1 (reading)")) title("classification based on rank ordering") ///
bar(1, bcolor(navy)) bar(2, bcolor(red*0.4) lcolor(red*0.9))  ///
ylab(0(0.2)1, nogrid) blabel(bar, position(outside) format(%9.2f)) graphregion(color(white)) saving(f1, replace) nodraw

如果我理解你的问题,这可能就是你想要的

ggplot(data = df3, 
aes(interaction(menutype,Belief),  #get combination of groups
prop , fill = Belief) + 
geom_bar(stat = "identity", position = "dodge")+
scale_x_discrete(labels = levels(df3$menutype)) # adds clean label to x

生成一些假数据:

set.seed(101)
df <- data.frame("menutype" = rep(c("GUILT", "SSB0", "SSB1", "FLEX0", "FLEX1", "STD", "FLEX01", "test1","test2", "test3", "test4", "test5"), each = 2), 
                 "value" = sample(c(2:10), 24, replace = TRUE),
                 "group" = rep(c("Not learn", "Learn"), times = 12))

制作情节:

library(ggplot2)
ggplot(df, aes(menutype, value))+
  geom_bar(aes(fill = group, color = group), stat = "identity", position = position_dodge(0.8), width = 0.7 )+
  scale_fill_brewer(palette = "Set1")+
  theme_classic()

结果:

基本上,您需要 3 个变量:

  1. 你的value(y轴)
  2. 你的menutype(x轴)
  3. 颜色变量group