【问题标题】:Barplot for count data with multiple columns多列计数数据的条形图
【发布时间】:2021-07-17 21:36:22
【问题描述】:

我有一个看起来像这样的数据框:

包含数据的列:

Sex     Var1  Var2   Var3
Male     Yes    No    Yes
Female    No   Yes     No
Male      No    No    Yes
Female   Yes   Yes     No

我想在 X 轴上为每个变量(男性一个,女性一个)创建一个条形图,Y 轴上的“是”计数,我真的不知道如何。任何帮助将不胜感激。

【问题讨论】:

    标签: r dataframe ggplot2 count


    【解决方案1】:

    这是一个base R。使用rowSums 在仅选择“Var”列的逻辑矩阵上获取“是”的计数,然后按“性别”进行分组以使用rowsum 按性别汇总计数并使用barplot

    barplot(t(rowsum(rowSums(df1[-1] == 'Yes'), df1$Sex)))
    

    或者如果我们需要按条形图进行分组,请将其更改为

    barplot(t(rowsum(+(df1[-1] == 'Yes'), df1$Sex)), beside = TRUE,
        legend = TRUE, col = c('red', 'blue', 'green'))
    

    或者,如果我们更喜欢ggplot,则使用pivot_longer(来自tidyr)重塑为“长”格式,获取group_bysummarise 以返回“是”的计数并使用ggplot

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    df1 %>%
        pivot_longer(cols = -Sex) %>%
        group_by(Sex) %>%
        summarise(n = sum(value == 'Yes')) %>%
        ggplot(aes(x = Sex, y = n)) +
           geom_col()
    

    对于每个 'Var' 的条形

    df1 %>%
       pivot_longer(cols = -Sex) %>%
       group_by(Sex, name) %>%
       summarise(n = sum(value == 'Yes'), .groups = 'drop') %>% 
       ggplot(aes(x = Sex, y = n, fill = name)) + 
          geom_col(position = 'dodge') 
    

    -输出

    数据

    df1 <- structure(list(Sex = c("Male", "Female", "Male", "Female"), 
    Var1 = c("Yes", 
    "No", "No", "Yes"), Var2 = c("No", "Yes", "No", "Yes"), Var3 = c("Yes", 
    "No", "Yes", "No")), class = "data.frame", row.names = c(NA, 
    -4L))
    

    【讨论】:

    • @CarlV.Bresser 我也更新了基础 R 图
    【解决方案2】:

    这应该能让你继续前进。

    可重现示例的数据

    df <- tribble(
    ~Sex, ~Var1, ~Var2, ~Var3
    ,"Male",     "Yes",    "No",    "Yes"
    ,"Female",    "No",   "Yes",     "No"
    ,"Male",      "No",    "No",    "Yes"
    ,"Female",   "Yes",   "Yes",     "No"
    )
    

    总结数据集

    library(dplyr)
    
    df <- df %>% 
      group_by(Sex) %>%
      summarise(Var1_Y = sum(Var1 == "Yes")
                ,Var2_Y = sum(Var2 == "Yes")
                ,Var3_Y = sum(Var3 == "Yes")
                )
    

    这给了你

     df
    # A tibble: 2 x 4
      Sex    Var1_Y Var2_Y Var3_Y
      <chr>   <int>  <int>  <int>
    1 Female      1      2      0
    2 Male        1      0      2
    

    ggplot 最适合“长”数据。为此,我们使用 pivot_longer()。

    library(tidyr)
    df <- df %>% 
     pivot_longer(cols = -Sex, names_to = "Var", values_to = "Val")
    

    使它成为一个长数据框

    df 
    # A tibble: 6 x 3
      Sex    Var      Val
      <chr>  <chr>  <int>
    1 Female Var1_Y     1
    2 Female Var2_Y     2
    3 Female Var3_Y     0
    4 Male   Var1_Y     1
    5 Male   Var2_Y     0
    6 Male   Var3_Y     2
    

    有了这个,你可以很容易地用 ggplot 绘图 总计使用 geom_col() 而不是 geom_bar()

    library(ggplot)
    df %>% 
      ggplot(aes(x = Var, y = Val, fill = Sex)) + 
      geom_col(position = "dodge")    # dodge puts the bars next to each other
    

    【讨论】:

      【解决方案3】:

      这是另一个barplot 演示文稿,使用data.table + ggplot2,即,

      melt(setDT(df), id.var = "Sex")[
        ,
        .(N = sum(value == "Yes")),
        .(Sex, variable)
      ] %>%
        ggplot(aes(x = Sex, y = N, fill = variable)) +
        geom_bar(stat = "identity")
      

      给了

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        相关资源
        最近更新 更多