【问题标题】:Create a ggplot with percentages创建一个带有百分比的ggplot
【发布时间】:2021-10-14 14:49:05
【问题描述】:

我想制作一个 ggplot(线图),在 x 轴(y 轴)上显示每个时间步长的值(A、B、C、D)的百分比。当我融化我的数据框时,我只有两列;如何计算百分比?

数据结构:

样本数据:

structure(list(`09:20` = c("A", "A", "A", "A", "B", "C", "D"), 
    `09:30` = c("B", "B", "B", "A", "A", "C", "C"), `09:40` = c("C", 
    "C", "C", "B", "B", "D", "D"), `09:50` = c("A", "A", "A", 
    "D", "D", "D", "D")), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -7L), spec = structure(list(
    cols = list(`09:20` = structure(list(), class = c("collector_character", 
    "collector")), `09:30` = structure(list(), class = c("collector_character", 
    "collector")), `09:40` = structure(list(), class = c("collector_character", 
    "collector")), `09:50` = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

【问题讨论】:

    标签: r dataframe ggplot2


    【解决方案1】:

    在重塑为长格式后,使用例如group_by + summarise 计算每个时间和值的计数,然后按时间和绘图计算百分比:

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    d1 <- d %>% 
      pivot_longer(everything(), names_to = "time", values_to = "value") %>% 
      group_by(time, value) %>% 
      summarise(n = n()) %>%  
      mutate(pct = n / sum(n)) %>% 
      ungroup()
    #> `summarise()` has grouped output by 'time'. You can override using the `.groups` argument.
    
    ggplot(d1, aes(time, pct, color = value, group = value)) +
      geom_line() +
      scale_y_continuous(labels = scales::percent)
    

    【讨论】:

      猜你喜欢
      • 2021-06-24
      • 1970-01-01
      • 2020-04-18
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-04
      相关资源
      最近更新 更多