R堆叠条形图与组
Posted
技术标签:
【中文标题】R堆叠条形图与组【英文标题】:R stacked barplot with groups 【发布时间】:2016-11-30 03:42:15 【问题描述】:我想创建一个包含组的堆叠条形图,但我正在为此苦苦挣扎:
data <- data.frame(timeslot=c("0-1", "1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "10-11", "11-12", "12-13", "13-14", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", "20-21", "21-22", "22-23", "23-0"),
Start1=c(3,0,1,0,0,1,2,22,58,41,30,41,52,52,38,35,20,18,14,19,12,2,9,0),
Start2=c(0,0,0,0,0,0,0,13,23,11,11,15,19,13,10,13,14,5,4,7,4,3,2,0),
Stop1=c(0,0,0,0,0,0,22,17,21,30,29,40,38,43,44,24,40,32,31,22,12,7,12,0),
Stop2=c(0,0,0,0,0,1,5,12,17,12,6,6,17,14,15,9,11,9,11,7,9,3,4,0))
时隙表示一天 24 小时的 1 小时间隔。 现在,我希望每个间隔有两个堆叠条,即 0-1、1-2、...、23-0。
Start 和 Stop 中的数字是在给定时间间隔内启动和停止的进程的计数。
一个条应该是 Start1 和 Start2(图像中堆叠的蓝色条,Start1 为深蓝色,Start2 为浅蓝色)。 另一个条应该是 Stop1 和 Stop2(图像中堆叠的棕色条,浅棕色类似于 Stop2)。
Picture to show
这个question 很相似,但我无法让它与我的数据一起使用:
library(reshape2) # for melt
melted2 <- melt(data, "timeslot")
melted2$cat <- ''
melted2[melted$variable == 'value1',]$cat <- "Start1"
melted2[melted$variable == 'value2',]$cat <- "Start2"
melted2[melted$variable == 'value3',]$cat <- "Stop1"
melted2[melted$variable == 'value4',]$cat <- "Stop2"
ggplot(melted2, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ timeslot)
【问题讨论】:
不确定什么是 melted$variable,因为问题中没有提到。 【参考方案1】:我想这就是你想要的。如您所见,我通过“开始”和“停止”两个类别对四种变量类型进行了分类。
library(ggplot2)
data <- data.frame(timeslot=c("0-1", "1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "10-11", "11-12", "12-13", "13-14", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", "20-21", "21-22", "22-23", "23-0"),
Start1=c(3,0,1,0,0,1,2,22,58,41,30,41,52,52,38,35,20,18,14,19,12,2,9,0),
Start2=c(0,0,0,0,0,0,0,13,23,11,11,15,19,13,10,13,14,5,4,7,4,3,2,0),
Stop1=c(0,0,0,0,0,0,22,17,21,30,29,40,38,43,44,24,40,32,31,22,12,7,12,0),
Stop2=c(0,0,0,0,0,1,5,12,17,12,6,6,17,14,15,9,11,9,11,7,9,3,4,0))
library(reshape2) # for melt
melted <- melt(data, "timeslot")
melted$cat <- ''
melted[grep(melted$variable, pattern='Start'),]$cat <- "Start"
melted[grep(melted$variable, pattern='Stop'),]$cat <- "Stop"
melted$timeslot <- factor(melted$timeslot, levels=c("0-1", "1-2", "2-3", "3-4", "4-5", "5-6", "6-7", "7-8", "8-9", "9-10", "10-11", "11-12", "12-13", "13-14", "14-15", "15-16", "16-17", "17-18", "18-19", "19-20", "20-21", "21-22", "22-23", "23-0"))
ggplot(melted, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ timeslot) +
scale_fill_manual(values = c("royalblue3", "royalblue1", "#8B4513", "#B8860B")) +
theme(legend.position = "bottom", axis.text.x = element_text(size=7))
【讨论】:
非常感谢,太好了。是否可以防止时隙被打乱?它们应保持在 timeslot... 中给出的顺序 @Geronimo 这回答了你的问题吗?以上是关于R堆叠条形图与组的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用ggplot2可视化堆叠条形图,并在堆叠条形图上显示数据值实战
R语言ggplot2可视化:ggplot2可视化水平堆叠条形图并且在每个堆叠条形图的内部居中添加百分比文本标签信息
R可视化ggplot2绘制堆叠的条形图(Stacked Barplot)
R语言ggplot2可视化堆叠的条形图(stacked bar plot)并在每一个条形图的的中间添加对应的数值值标签定位在geom_col堆叠的条形图中的每个条形段的中间