创建一个带有百分比的ggplot

Posted

技术标签:

【中文标题】创建一个带有百分比的ggplot【英文标题】:Create a ggplot with percentages 【发布时间】: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"))

【问题讨论】:

【参考方案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)

【讨论】:

以上是关于创建一个带有百分比的ggplot的主要内容,如果未能解决你的问题,请参考以下文章

带有组和构面的堆叠 ggplot 条形图的百分比标签

如何使用ggplot2格式化带有轴标签和旋转的R中的雷达图

带有facet_grid的ggplot2中带有多个分类变量的堆积条形图

R Shiny ggplot 条形图和折线图,具有动态变量选择和 y 轴为百分比

在 r ggplot2 中为百分比值创建条形图

带有 facet_grid 的 ggplot2 中具有多个分类变量的堆积条形图