【问题标题】:Barplots with multiple factor groupings and mean of variable across those factors具有多个因子分组和这些因子变量平均值的条形图
【发布时间】:2017-10-08 00:16:32
【问题描述】:

我正在尝试创建一个条形图,显示工会和非工会工人的平均小时工资,按单身或已婚分组,按大学毕业生或非大学毕业生分组。虽然我设法用两个因子分组构建了一个可以通过的条形图,但我无法弄清楚如何使用三个因子分组来做到这一点。我看到的具有三个因子的示例仅关注频率计数,因此我不确定如何将所有因子的另一个变量的平均值合并到图中。我想要创建的是看起来像这样的东西(在 Stata 中创建): Average Hourly Wage by Union Status, Marital Status, and College Graduation 我的代码如下所示:

levelbar = tapply(wage, list(as.factor(union), as.factor(married), 
as.factor(collgrad)), mean)
par(mfrow = c(1, 2))
barplot(levelbar, beside = TRUE)
barplot(t(levelbar), beside = TRUE)

但是,当我运行它时,我收到错误:

Error in barplot.default(levelbar, beside = TRUE) : 
'height' must be a vector or a matrix

对此的任何帮助将不胜感激。我确信 ggplot 在这里可能有用,但我没有大量使用该软件包的经验。

【问题讨论】:

    标签: r plot bar-chart factors tapply


    【解决方案1】:

    这是一个使用 ggplot 和内置数据集 Titanic 的可重现示例。

    请注意,我们首先计算均值并使用stat = identity 确保我们将这些均值纳入图中。

    # Format the Titanic dataframe
    Titanic_df <- Titanic %>% as_tibble()
    
    # Make Class, Sex, Age, and Survived factors
    for (col in c("Class", "Sex", "Age", "Survived")) {
      Titanic_df[[col]] <- factor(Titanic_df[[col]])
    }
    
    # Get by group means
    means <- Titanic_df %>% 
      group_by(Class, Sex, Survived) %>% 
      summarise(
        mean_n = mean(n)
      )
    
    # Plot: facets are the Classes, bar colors are the two Sexes, and the groupings in each facet are Survived vs. Not Survived
    ggplot(data = means) +
      geom_bar(aes(x = Survived, y = mean_n, fill = Sex), stat = "identity", position = "dodge") +
      facet_wrap(~ Class)
    

    【讨论】:

    • 谢谢!如果我想消除出现 b/c 联合因子级别具有 NA 的第三列,我会将它放在哪里?我试过means &lt;- nlsw_df %&gt;% na.omit() %&gt;% group_by(union, married, collgrad) %&gt;% summarise( mean_wage = mean(wage) ) 我试过ggplot(data = na.omit(means)) + geom_bar(aes(x = collgrad, y = mean_wage, fill = union), stat = "identity", position = "dodge") + facet_wrap(~ married) 我试过for (col in c("union", "married", "collgrad")) { nlsw_df[[col]] &lt;- factor(nlsw_df[[col]], exclude = NA) }
    • 听起来仍然存在 NA 因子水平,即使您已经摆脱了 NA 值。在 na.omit() 之后链接 droplevels()(或者 drop_na(union),如果你只想丢弃联合列中带有 NA 的行)应该可以解决问题。
    • 您好阿曼达,感谢您的回复。我认为你说这是正确的做法是对的,但由于某种原因我无法让它发挥作用。尽管在 na.omit() 之后链接了 droplevels(),第三个未使用的 NA 条仍然显示在图表上。我运行的代码是:means &lt;- nlsw_df %&gt;% na.omit(union) %&gt;% droplevels(union) %&gt;% group_by(union, married, collgrad) %&gt;% summarise( mean_wage = mean(wage) ) ggplot(data = means) + geom_bar(aes(x = collgrad, y = mean_wage, fill = union), stat = "identity", position = "dodge") + facet_wrap(~ married)
    • 如果您在 x 轴上得到 NA,这意味着您没有在 collgrad 中删除 NA。我会在整个数据帧上使用droplevels(),所以means &lt;- nlsw_df %&gt;% na.omit() %&gt;% droplevels() %&gt;% group_by(union, married, collgrad) %&gt;% summarise( mean_wage = mean(wage) )。这应该会降低所有 NA 和所有 NA 级别。如果这不起作用,那么如果您可以展示数据外观的示例(前几行或可以很好地模拟它的内容),则更容易弄清楚发生了什么。
    • @amanda 使用 titanic 和您的代码我收到此错误消息错误:至少一层必须包含所有构面变量:Class。 * 缺少绘图 Class * 缺少第 1 层 Class 回溯:1. (function (x, ...) ... 2. ggplot2:::print.ggplot(x) 4. ggplot2:::ggplot_build .ggplot(x) 5. layout$setup(data, plot$data, plot$plot_env) 6. ggplot2:::f(..., self = self) 7. self$facet$compute_layout(data, self$facet_params ) 8. ggplot2:::f(...) 11. ggplot2::combine_vars(data, params$plot_env, vars, drop = params$drop)
    猜你喜欢
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2014-04-13
    相关资源
    最近更新 更多