Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Stacked "Progress" Bar Chart in R with ggplot

I am looking for a way to use ggplot to create a variation of a stacked bar chart. More of like a "progress bar" chart. I have Dates on the x axis, and a categorical variable "activity" on the y axis. Each activity has a "Red", "Yellow", or "Green" status. I want to plot the status of each activity over time. Problem is I don't have a numeric input to supply. And the dates are displaying weird and also not in chronological order. Hopefully you can get the idea of what I'm trying to do by looking at my plot and code below:

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

Here is my code to generate the plot.

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"))

enter image description here

like image 644
bodega18 Avatar asked Jul 31 '26 12:07

bodega18


1 Answers

One option to achieve your desired result would be to switch to geom_rect.

As you have a categorial column to be mapped on y I transform it to a numeric which requires to put the labels back on the now continuous scale.

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"))

EDIT

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"))

like image 181
stefan Avatar answered Aug 03 '26 02:08

stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!