【问题标题】:Create Stacked "Progress" Bar Chart in R with ggplot使用 ggplot 在 R 中创建堆叠的“进度”条形图
【发布时间】:2022-01-15 10:37:42
【问题描述】:

我正在寻找一种方法来使用 ggplot 创建堆叠条形图的变体。更像是一个“进度条”图表。我在 x 轴上有日期,在 y 轴上有一个分类变量“活动”。每个活动都有“红色”、“黄色”或“绿色”状态。我想随着时间的推移绘制每个活动的状态。问题是我没有要提供的数字输入。日期显示很奇怪,也不按时间顺序排列。希望您可以通过查看下面的情节和代码来了解我正在尝试做什么:

activity    date     status
a          11-10-21   red
a          11-17-21   red
a          11-24-21   yellow
a          12-01-21   green
b          11-10-21   red
b          11-17-21   yellow
b          11-24-21   green
b          12-01-21   green
c          11-10-21   yellow
c          11-17-21   green
c          11-24-21   green
c          12-01-21   green

这是我生成绘图的代码。

activity <- c("a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c")
date <- c("2021-11-10", "2021-11-17", "2021-11-24", "2021-12-01", "2021-11-10", "2021-11-17", 
"2021-11-24", "2021-12-01", "2021-11-10", "2021-11-17", "2021-11-24", "2021-12-01")
status <- c("red", "red", "yellow", "green", "red", "yellow", "green", "green", "yellow", 
"green", "green", "green")


df <- data.frame(activity, date, status)

df$activity <- as.factor(df$activity)
df$date <- as.Date(df$date)
df$status <- as.factor(df$status)

ggplot(df, aes(x=date, y=activity, fill = status)) + geom_bar(stat = "identity") +
scale_fill_manual(values = c("#6FC750", "#CC5939", "#D1CB28"))

【问题讨论】:

    标签: r date ggplot2 dplyr visualization


    【解决方案1】:

    实现您想要的结果的一个选择是切换到geom_rect

    由于您有一个类别列要映射到 y,因此我将其转换为一个数字,这需要将标签放回现在的连续刻度上。

    library(ggplot2)
    library(dplyr)
    library(lubridate)
    
    df <- df %>% 
      mutate(date_end = date + lubridate::days(7))
    
    width = .6
    
    breaks <- seq(levels(factor(activity)))
    labels <- levels(factor(activity))
    ggplot(df, aes(fill = status)) + 
      geom_rect(aes(xmin = date, xmax = date_end, 
                    ymin = as.numeric(factor(activity))  - width / 2, 
                    ymax = as.numeric(factor(activity))  + width / 2)) +
      scale_y_continuous(breaks = breaks, labels= labels) +
      scale_fill_manual(values = c("#6FC750", "#CC5939", "#D1CB28"))
    

    编辑

    set.seed(42)
    
    df <- df %>% 
      mutate(date_end = date + lubridate::days(sample(3:7, nrow(.), replace = TRUE)))
    
    width = .6
    
    breaks <- seq(levels(factor(activity)))
    labels <- levels(factor(activity))
    ggplot(df, aes(fill = status)) + 
      geom_rect(aes(xmin = date, xmax = date_end, 
                    ymin = as.numeric(factor(activity))  - width / 2, 
                    ymax = as.numeric(factor(activity))  + width / 2)) +
      scale_y_continuous(breaks = breaks, labels= labels) +
      scale_fill_manual(values = c("#6FC750", "#CC5939", "#D1CB28"))
    

    【讨论】:

    • 非常感谢,成功了。有没有办法我可以用黑色轮廓勾勒出构成整个条的每个单独的矩形?
    • 当然:geom_rect(aes(...), color = "black")
    • 还有一件事。有没有办法填补矩形之间的任何空白(例如,如果日期不是全部相隔 1 周)?
    • 是的。需要的是开始和结束日期,即。而不是固定的date_end,您可以用可变的结束日期填充此列。查看我的编辑,其中我使用了一些随机结束日期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2021-07-31
    • 1970-01-01
    • 2013-03-03
    • 2016-01-29
    相关资源
    最近更新 更多