如何叠加多个图

时间:2019-05-30 18:44:01

标签: r ggplot2

我想在一张图中绘制3种不同的占用模式:a。当男性和女性都在工作时; b。)只有男性在工作; c。)只有女性在工作。

我使用facet创建了3个不同的data.frames

第一个数据帧(男性和女性都在工作)的结构是:

   structure(list(time = c("04:00", "04:15", "04:30", "04:45", "05:00", 
"05:15", "05:30", "05:45", "06:00", "06:15", "06:30", "06:45", 
"07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", 
"08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", 
"10:30", "10:45", "11:00", "11:15"), day = c("Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday"
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Shifts on Monday", "Shifts on Tuesday", 
"Shifts on Wednesday", "Shifts on Thursday", "Shifts on Friday", 
"Shifts on Saturday", "Shifts on Sunday"), class = "factor"), 
    value = c(102, 110, 116, 120, 148, 152, 188, 204, 318, 348, 
    418, 442, 752, 816, 1064, 1144, 1836, 1942, 2402, 2524, 3240, 
    3324, 3486, 3520, 3594, 3592, 3596, 3568, 3594, 3626), size_f = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Monday", 
    "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Sunday"), class = "factor")), row.names = c(NA, 30L), class = "data.frame")

第二个data.frame(男性正在工作)的结构是:

dput(head(melted_WkSTiWeekM , 30))
structure(list(time = c("04:00", "04:15", "04:30", "04:45", "05:00", 
"05:15", "05:30", "05:45", "06:00", "06:15", "06:30", "06:45", 
"07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", 
"08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", 
"10:30", "10:45", "11:00", "11:15"), day = c("Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday"
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Shifts on Monday", "Shifts on Tuesday", 
"Shifts on Wednesday", "Shifts on Thursday", "Shifts on Friday", 
"Shifts on Saturday", "Shifts on Sunday"), class = "factor"), 
    value = c(56, 58, 64, 68, 84, 84, 104, 114, 172, 196, 224, 
    238, 410, 442, 560, 604, 930, 968, 1188, 1230, 1576, 1620, 
    1692, 1706, 1740, 1746, 1752, 1740, 1736, 1756), size_f = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Monday", 
    "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Sunday"), class = "factor")), row.names = c(NA, 30L), class = "data.frame")

第二个data.frame(女性正在工作)的结构是:

structure(list(time = c("04:00", "04:15", "04:30", "04:45", "05:00", 
"05:15", "05:30", "05:45", "06:00", "06:15", "06:30", "06:45", 
"07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", 
"08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", 
"10:30", "10:45", "11:00", "11:15"), day = c("Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday"
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L), .Label = c("Shifts on Monday", "Shifts on Tuesday", 
"Shifts on Wednesday", "Shifts on Thursday", "Shifts on Friday", 
"Shifts on Saturday", "Shifts on Sunday"), class = "factor"), 
    value = c(46, 52, 52, 52, 64, 68, 84, 90, 146, 152, 194, 
    204, 342, 374, 504, 540, 906, 974, 1214, 1294, 1664, 1704, 
    1794, 1814, 1854, 1846, 1844, 1828, 1858, 1870), size_f = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Monday", 
    "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Sunday"), class = "factor")), row.names = c(NA, 30L), class = "data.frame")

此后,我创建了一个

melted_WkSTiWeekAll=cbind(melted_WkSTiWeek,melted_WkSTiWeekM,melted_WkSTiWeekF)



structure(list(time = c("04:00", "04:15", "04:30", "04:45", "05:00"
), day = c("Monday", "Monday", "Monday", "Monday", "Monday"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("Shifts on Monday", 
    "Shifts on Tuesday", "Shifts on Wednesday", "Shifts on Thursday", 
    "Shifts on Friday", "Shifts on Saturday", "Shifts on Sunday"
    ), class = "factor"), value = c(102, 110, 116, 120, 148), 
    size_f = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("Monday", 
    "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", 
    "Sunday"), class = "factor"), time = c("04:00", "04:15", 
    "04:30", "04:45", "05:00"), day = c("Monday", "Monday", "Monday", 
    "Monday", "Monday"), variable = structure(c(1L, 1L, 1L, 1L, 
    1L), .Label = c("Shifts on Monday", "Shifts on Tuesday", 
    "Shifts on Wednesday", "Shifts on Thursday", "Shifts on Friday", 
    "Shifts on Saturday", "Shifts on Sunday"), class = "factor"), 
    value = c(56, 58, 64, 68, 84), size_f = structure(c(1L, 1L, 
    1L, 1L, 1L), .Label = c("Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday", "Sunday"), class = "factor"), 
    time = c("04:00", "04:15", "04:30", "04:45", "05:00"), day = c("Monday", 
    "Monday", "Monday", "Monday", "Monday"), variable = structure(c(1L, 
    1L, 1L, 1L, 1L), .Label = c("Shifts on Monday", "Shifts on Tuesday", 
    "Shifts on Wednesday", "Shifts on Thursday", "Shifts on Friday", 
    "Shifts on Saturday", "Shifts on Sunday"), class = "factor"), 
    value = c(46, 52, 52, 52, 64), size_f = structure(c(1L, 1L, 
    1L, 1L, 1L), .Label = c("Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday", "Sunday"), class = "factor")), row.names = c(NA, 
5L), class = "data.frame")

变量time, day, variable and size_f相似。

我想在同一张图中一起绘制3张图。但是我不知道如何做到这一点

在下面的图中使用建议的facet

情节1

enter image description here

情节2

enter image description here

情节3

enter image description here

如何前进

我想创建一个像这样的锅

Lesnard

1 个答案:

答案 0 :(得分:1)

您可以执行此操作,但是将数据框合并在一起,并分别将其标记为AllMaleFemale。然后,您可以在ggplot函数中使用该标签进行分组(着色)。

使用geom_barposition = "identity",您会在每个广告的上方看到条形图,以消除条形图之间的间隔(以模仿密度图的外观)width设置为{{1 }}。 1可用于更改alpha颜色的透明度。

要获取一周中每一天的单独图表,请使用fill

facet_wrap(~day)

请注意,由于提供的数据仅包含星期一的条目,因此在这里我们只得到一个library(tidyverse) melted_WkSTiWeekAll <- bind_rows(list(All=melted_WkSTiWeek, Male=melted_WkSTiWeekM, Female=melted_WkSTiWeekF), .id = 'source') ggplot(melted_WkSTiWeekAll, aes(x=time, y=value, fill=source)) + geom_bar(stat="identity", position = "identity", alpha=.3, width=1) + facet_wrap(~day) reprex package(v0.3.0)于2019-05-31创建