【问题标题】:Multigroup frequency with ggplotggplot的多组频率
【发布时间】:2020-05-09 16:54:39
【问题描述】:

我正在尝试在 R 中 replicate this histogram

这是模拟我的数据集的方法:

    dft <- data.frame(
  menutype =  sample(c(1,2,4,5,6,8,12), 120, replace = T),
  Belief = sample(c(0,1), 120, replace = T),
  Choice = sample(c(0,1), 120, replace = T)
)

这是我的代码:

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    library(MASS)


    df <- data.frame(
  menutype =  factor(df$menutype, labels = c("GUILT" , "SSB0", "SSB1", "FLEX0", "FLEX1", "STD", "FLEX01"),
                     levels = c(1,2,4,5,6,8,12)),
  Belief = factor(df$belieflearn, levels = c(1), labels= c("Believe Learn")), #Interested only in this condition
  Choice = factor(df$learned, levels = c(1), labels= c("Learn")) #Same here
)


    df1 <- rbind(na.omit(df %>%
                           count(Belief, menutype) %>%
                           group_by(menutype) %>% 
                           mutate(prop = n / sum(n))),
                 na.omit(df %>%
                           count(Choice, menutype) %>%
                           group_by(menutype) %>% 
                           mutate(prop = n / sum(n))))



    test <- paste(df1$Belief[1:6],paste(df1$Choice[7:13]))
test[1:6] <- paste(df1$Belief[1:6])
test[7:13] <- paste(df1$Choice[7:13])

df1$combine <- paste(test)

    ggplot(data = df1, aes(menutype, prop, fill = combine)) + 
      labs(title = "Classification based on rank ordering\n", x = "", y = "Fraction of subjects", fill = "\n") +
      geom_bar(stat = "identity", position = "dodge")+
      theme_bw() +
      theme(legend.position="bottom", plot.title = element_text(hjust = 0.5)) #Centering of the main title+
    #geom_text(aes(label="ok"), vjust=-0.3, size=3.5)+

问题是它或多或少地工作,我几乎是我想要的getting the graph,但这是一种解决方法,但仍然存在一些错误。事实上,例如,我为 STD (0.10) 设置了相同的值,而它应该是 0 和 0.10,就像在原始图中一样。

我最想做的是有两个不同的数据帧,一个是menutypeBelief,另一个是menutypeChoice,然后像我一样,计算特定的比例menutype 上的每个后一个变量的模态,最后将其绘制为直方图,就像原始研究中的 the graph 一样。此外,我希望在每个条形上方将比例作为分数,但这是可选的。

有人可以帮我解决这个问题吗?我真的很难让它发挥作用。

提前致谢!

编辑:我认为问题在于fill =。我想为每个条指定我想要的变量(例如,fill = df2$Belief &amp; df2$Choice),但我不知道如何继续。

【问题讨论】:

  • 我们需要一些数据来帮助您。您可以创建一个包含所有感兴趣变量、计数和百分比的表格,然后使用 facet_wrap (sthda.com/english/wiki/…) 拆分图表。您可以使用 geom_text 在条形顶部显示分数 (sthda.com/english/wiki/…),但您必须在另一列中以文本形式创建这些分数。
  • 哦,是的!对不起。刚刚编辑了我的帖子。问题是我希望将两个变量放在同一个图中,而不是拆分。不过谢谢!
  • 很遗憾,我无法从头到尾使用您的流程,因为存在一些错误。但是,我将发布一些内容,以帮助您了解包含所有统计信息的数据集的外观,以便以简单的方式按照您想要的方式绘制它们......
  • ....请注意,我的过程只是一个示例。你的会更复杂,但这里的关键是看到你需要(a)你的比例,(b)将创建条形图的变量,(c)将分割你的图的变量,(d)其他任何东西您想在同一数据集中的绘图上显示。

标签: r ggplot2 histogram


【解决方案1】:
library(tidyverse)

set.seed(10)

# example data frame
df <- data.frame(
  menutype =  sample(c(1,2,4,5,6,8,12), 120, replace = T),
  Belief = sample(c(0,1), 120, replace = T),
  Choice = sample(c(0,1), 120, replace = T)
)

# calculate all metrics based on all variables you want to plot in a tidy way
df_plot = df %>%
  group_by(Choice) %>%
  count(menutype, Belief) %>%
  mutate(prop = n / sum(n),
         prop_text = paste0(n, "/", sum(n))) %>%
  ungroup()

# barplots using one variable and split plots using another variable
df_plot %>%
  mutate(Belief = factor(Belief),
         menutype = factor(menutype)) %>%
  ggplot(aes(menutype, prop, fill = Belief))+
  geom_col(position = "dodge")+
  facet_wrap(~Choice, ncol=1)+
  geom_text(aes(label=prop_text), position = position_dodge(1), vjust = -0.5)+
  ylim(0,0.2)

【讨论】:

  • 感谢您的帮助,但我想要得到的是在同一个图表上拥有两个变量(选择和信念)。换句话说,对于每种菜单类型,蓝色条是“信念”的比例,红色是“选择”的比例。但无论如何,非常感谢,我会尝试调整你的代码以达到为我工作。
  • 这听起来像是一个列重塑。在我的示例中,我使用变量Belief,其值为01,但您应该创建一个名为group(或您想要的任何名称)的变量,其值为ChoiceBelief。然后你可以像我上面的例子一样绘制。
  • 这将创建一个与您的数据集相比长度更大的变量。此外,这些值不会是BeliveChoice,而是这些列的值。尝试从tidyr找到一些重塑函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2017-08-25
  • 1970-01-01
  • 2017-04-23
  • 2021-11-01
相关资源
最近更新 更多