【问题标题】:How to group data and then draw bar chart in ggplot2如何对数据进行分组然后在ggplot2中绘制条形图
【发布时间】:2014-11-09 21:09:30
【问题描述】:

我有 3 列的数据框 (df),例如

NUMERIC1:      NUMERIC2:      GROUP(CHARACTER):
100            1               A
200            2               B
300            3               C
400            4               A

我想按 GROUP(CHARACTER) 对 NUMERIC1 进行分组,然后计算每个组的平均值。 类似的东西:

mean(NUMERIC1):  GROUP(CHARACTER):
250                  A
200                  B
300                  C

最后,我想使用 ggplot2 绘制条形图,在 x 轴上具有 GROUP(CHARACTER),在 y 轴上具有 =nd mean(NUMERIC)。 它应该看起来像:

我用过

mean <- tapply(df$NUMERIC1, df$GROUP(CHARACTER), FUN=mean)

但我不确定它是否可以,即使可以,我也不知道我接下来应该做什么。

【问题讨论】:

    标签: r ggplot2 grouping bar-chart


    【解决方案1】:

    这就是 stat_summmary(...) 的用途:

    colnames(df) <- c("N1","N2","GROUP")
    library(ggplot2)
    ggplot(df) + stat_summary(aes(x=GROUP,y=N1),fun.y=mean,geom="bar", 
                              fill="lightblue",col="grey50")
    

    【讨论】:

    • +1,关于 stat_summary 的要点!所以这是生成该条形图的 ggplot2 惯用方式。
    【解决方案2】:

    尝试类似:

    res <- aggregate(NUMERIC1 ~ GROUP, data = df, FUN = mean)
    ggplot(res, aes(x = GROUP, y = NUMERIC1)) + geom_bar(stat = "identity")
    

    数据

    df <- structure(list(NUMERIC1 = c(100L, 200L, 300L, 400L), NUMERIC2 = 1:4, 
        GROUP = structure(c(1L, 2L, 3L, 1L), .Label = c("A", "B", 
        "C"), class = "factor")), .Names = c("NUMERIC1", "NUMERIC2", 
    "GROUP"), class = "data.frame", row.names = c(NA, -4L))
    

    【讨论】:

      【解决方案3】:

      我建议如下:

      #Imports; data.table, which allows for really convenient "apply a function to
      #"each part of a df, by unique value", and ggplot2
      library(data.table)
      library(ggplot2)
      
      #Convert df to a data.table. It remains a data.frame, so any function that works
      #on a data.frame can still work here.
      data <- as.data.table(df)
      
      #By each unique value in "CHARACTER", subset and calculate the mean of the
      #NUMERIC1 values within that subset. You end up with a data.frame/data.table
      #with the columns CHARACTER and mean_value
      data <- data[, j = list(mean_value = mean(NUMERIC1)), by = "CHARACTER"]
      
      #And now we play the plotting game (the plotting game is boring, lets
      #play Hungry Hungry Hippos!)
      plot <- ggplot(data, aes(CHARACTER, mean_value)) + geom_bar()
      
      #And that should do it.
      

      【讨论】:

      • 我已经安装并加载了 ggplot2,但是当我尝试绘制绘图时,我可以看到:“错误:找不到函数“ggplot2””
      • 那是因为没有这个功能。立即尝试
      【解决方案4】:

      这是使用dplyr 创建摘要的解决方案。在这种情况下,摘要是在 ggplot 中动态创建的,但您也可以先创建一个单独的摘要数据框,然后将其提供给 ggplot

      library(dplyr)
      library(ggplot2)
      
      ggplot(df %>% group_by(GROUP) %>% 
               summarise(`Mean NUMERIC1`=mean(NUMERIC1)),
             aes(GROUP, `Mean NUMERIC1`)) + 
        geom_bar(stat="identity", fill=hcl(195,100,65))
      

      由于您绘制的是平均值而不是计数,因此使用点而不是条可能更有意义。例如:

      ggplot(df %>% group_by(GROUP) %>% 
               summarise(`Mean NUMERIC1`=mean(NUMERIC1)),
             aes(GROUP, `Mean NUMERIC1`)) + 
        geom_point(pch=21, size=5, fill="blue") + 
        coord_cartesian(ylim=c(0,310))
      

      【讨论】:

        【解决方案5】:

        当您可以使用自己的代码和条形图做同样的事情时,为什么要使用 ggplot:

        barplot(tapply(df$NUMERIC1, df$GROUP, FUN=mean))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-10
          • 2020-02-22
          • 1970-01-01
          • 2014-01-17
          • 2020-05-29
          • 1970-01-01
          相关资源
          最近更新 更多