我正在寻找一种使用ggplot创建堆叠条形图变体的方法。更像是“进度条”图表。 我的x轴上有日期,y轴上有一个分类变量“activity”。每个活动都有“红色”,“黄色”或“绿色”的状态。我想绘制每个活动的状态随时间的变化情况。问题在于我没有提供数字输入,并且日期的显示方式很奇怪,而且也不是按时间顺序显示。希望您可以通过查看下面的图表和代码来了解我尝试做什么的想法:
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"))
geom_rect(aes(...), color = "black")
- stefan