Home > OS >  Create Stacked "Progress" Bar Chart in R with ggplot
Create Stacked "Progress" Bar Chart in R with ggplot

Time:12-11

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

CodePudding user response:

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

  • Related