【问题标题】:Create multigroup histograms side by side in R在 R 中并排创建多组直方图
【发布时间】:2020-05-08 19:59:27
【问题描述】:

我正在尝试在 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

想法如下:首先,我只选择变量“menuselection == 3”所在的变量。然后,对于这些变量,对于图表上分别对应于“GUILT”、“SSB0”... (所以如果belieflearn == 1)和频率如果玩家选择选项1(所以如果learned == 1)。

我认为这里需要使用factor(),但我不太了解如何使用。我关注了this thread,我试过这个:

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() 但没有成功。

我也没有指定 menuselection == 3。也许应该用循环来做?

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

另外,我对 R(以及 StackOverflow)还很陌生,所以如果我应该在这个线程中添加一些东西,请告诉我!

感谢您的帮助。

编辑:我在原始研究中找到了用于生成图形的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

【问题讨论】:

    标签: r histogram


    【解决方案1】:

    如果我理解了你的问题,这可能就是你所追求的

    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
    

    【讨论】:

    • 感谢您的帮助,但很遗憾我没有得到正确的结果。
    【解决方案2】:

    生成一些假数据:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      相关资源
      最近更新 更多