在甘特图上绘制许多折线图,快速/可扩展

时间:2017-10-05 22:02:19

标签: r ggplot2 time-series gantt-chart plotrix

目标图表(用Photoshop制作)

监控看门人进出大厅的情况。 Blue是Janitor 1,Brown是Janitor 2. Line代表大厅里的客人数量(比例相对于自身,即ymin=0, ymax=max.visitor.value,没有明确的轴)。颜色代表(cum. janitor time)/(cum. # of guests),即如果混乱堆积,线开始变红。 enter image description here

我在哪里:(R with plotrix :: gantt.chart) enter image description here

问题

我希望用R完全达到目标(每天增加渐变线)。我不知道如何继续 - 我想到以下两种情况之一:1)用较低的杠杆图形添加它们并手动安排它们,因为plotrix基于基础图形或2)远离plotrix并使用基于网格的包,它可能比前者更可读/更高级别(?)。

问题

我试图通过查看SO thread讨论各种制作方法的方法来寻找基于网格的包。我尝试过使用情节,但它似乎并没有支持多个时间块'在相同的任务'。我想暂停一下,然后把更多的时间花在其他包装上,并且可能会遇到死胡同(考虑添加所有的线条图),而不知道优秀的方法。

作为最后的手段,我可​​以考虑逐一创建折线图并将它们拼接到R外的甘特图。

数据样本

甘特图数据(随机样本,为简洁起见,仅在周一和周二)

   janitor.type weekday    dummy.start.time      dummy.end.time
        <dbl>   <chr>              <dttm>              <dttm>
 1          1  Monday 1970-01-01 18:01:20 1970-01-01 18:06:50
 2          1  Monday 1970-01-01 18:08:10 1970-01-01 18:11:52
 3          1  Monday 1970-01-01 17:22:00 1970-01-01 17:23:00
 4          1  Monday 1970-01-01 11:39:40 1970-01-01 11:41:58
 5          2  Monday 1970-01-01 19:35:40 1970-01-01 19:40:40
 6          1  Monday 1970-01-01 15:23:00 1970-01-01 15:24:12
 7          1  Monday 1970-01-01 11:54:50 1970-01-01 12:00:20
 8          1 Tuesday 1970-01-01 17:23:00 1970-01-01 18:18:18
 9          2 Tuesday 1970-01-01 19:25:00 1970-01-01 19:39:18
10          1 Tuesday 1970-01-01 16:40:10 1970-01-01 17:09:10
11          1 Tuesday 1970-01-01 14:16:50 1970-01-01 14:19:38
12          2 Tuesday 1970-01-01 09:27:00 1970-01-01 09:30:30
13          1 Tuesday 1970-01-01 14:08:40 1970-01-01 14:13:40
14          1 Tuesday 1970-01-01 11:12:40 1970-01-01 11:13:40

> dput(gantt)

structure(list(asset.type = c(1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 
1, 1, 1), weekday = c("Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Tuesday", "Tuesday", "Tuesday"), dummy.start.time = structure(c(82880, 
83290, 80520, 59980, 88540, 73380, 60890, 80580, 87900, 78010, 
69410, 52020, 68920, 58360), class = c("POSIXct", "POSIXt"), tzone = ""), 
    dummy.end.time = structure(c(83210, 83512, 80580, 60118, 
    88840, 73452, 61220, 83898, 88758, 79750, 69578, 52230, 69220, 
    58420), class = c("POSIXct", "POSIXt"), tzone = "")), row.names = c(NA, 
-14L), vars = "weekday", drop = TRUE, .Names = c("asset.type", 
"weekday", "dummy.start.time", "dummy.end.time"), indices = list(
    0:6, 7:13), group_sizes = c(7L, 7L), biggest_group_size = 7L, labels = structure(list(
    weekday = c("Monday", "Tuesday")), row.names = c(NA, -2L), class = "data.frame", vars = "weekday", drop = TRUE, .Names = "weekday"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

访客

Monday          Tuesday 
9:00:00 AM  138 9:00:00 AM  153
10:00:00 AM 251 10:00:00 AM 299
11:00:00 AM 432 11:00:00 AM 479
12:00:00 PM 560 12:00:00 PM 453
1:00:00 PM  555 1:00:00 PM  535
2:00:00 PM  475 2:00:00 PM  383
3:00:00 PM  448 3:00:00 PM  416
4:00:00 PM  469 4:00:00 PM  417
5:00:00 PM  459 5:00:00 PM  519
6:00:00 PM  403 6:00:00 PM  384
7:00:00 PM  290 7:00:00 PM  278
8:00:00 PM  120 8:00:00 PM  116
9:00:00 PM  29  9:00:00 PM  34

dput(访客)

structure(list(weekday = c("Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday", "Monday", "Monday", "Monday", 
"Monday", "Monday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", "Tuesday", 
"Tuesday", "Tuesday", "Tuesday"), time = structure(c(50400, 54000, 
57600, 61200, 64800, 68400, 72000, 75600, 79200, 82800, 86400, 
90000, 93600, 50400, 54000, 57600, 61200, 64800, 68400, 72000, 
75600, 79200, 82800, 86400, 90000, 93600), class = c("POSIXct", 
"POSIXt"), tzone = ""), count = c(138L, 251L, 432L, 560L, 555L, 
475L, 448L, 469L, 459L, 403L, 290L, 120L, 29L, 153L, 299L, 479L, 
453L, 535L, 383L, 416L, 417L, 519L, 384L, 278L, 116L, 34L)), .Names = c("weekday", 
"time", "count"), row.names = c(NA, -26L), class = "data.frame")

plotrix :: gantt.chart代码

# Set up variables for gantt chart

labels <- gantt$weekday
starts <- gantt$dummy.start.time
ends <- gantt$dummy.end.time
priorities <- as.numeric(gantt$asset.type)

Ymd.format <- "%Y/%m/%d %H:%M:%S"

# Feed variables to chart parameters

gantt.info <- list(
  labels = labels,
  starts = starts,
  ends = ends,
  priorities = priorities
)

# Define chart intervals

hours <- seq(
  as.POSIXct("1970/01/01 09:00:00", format = Ymd.format),
  as.POSIXct("1970/01/01 21:00:00", format = Ymd.format),
  by = "hour"
)

# Define labels for vgridlab

hourslab <- format(hours, format = "%H")

# Define vertical gridline

vgridpos <- as.POSIXct(hours, format = Ymd.format)
vgridlab <- hourslab

# Optional coloring

colfunc <- colorRampPalette(c("#00bff3", "#362f2d"))

# Define timeframe on x axis

timeframe <-
  as.POSIXct(c("1970/01/01 09:00:00", "1970/01/01 21:00:00"), format = Ymd.format)

# Create the chart

main = ""

test <- gantt.chart(
  gantt.info,
  taskcolors = colfunc(2),
  xlim = timeframe,
  main = main,
  priority.legend = F,
  vgridpos = vgridpos,
  vgridlab = vgridlab,
  hgrid = TRUE,
  half.height = 0.125,
  time.axis = 1
)

1 个答案:

答案 0 :(得分:1)

我使用了ggplot2。为了能够使用2种不同的颜色设置 - 一种用于甘特图资产类型和另一种访客数量,我选择了两种颜色类型,一种能够采用填充颜色:geom_rect,另一种颜色为:{{1} }。

我使用网格刻面来分隔工作日,因为甘特图和访客没有共同的y轴。

可以通过geom_line(连续)或scale_colour_gradient(离散)设置颜色和填充调色板,或者利用颜色冲泡调色板scale_fill_manual(连续)和{{1离散的。我使用下面每个例子来复制给定甘特图的颜色方案。

scale_colour_distiller

注意:您可以通过调整scale_fill_brewer的{​​{1}}和# factor weekdays and set levels to correct order gantt$weekday <- factor(gantt$weekday, levels=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) visitors$weekday <- factor(visitors$weekday, levels=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")) # factor gantt$asset.type as this represent categorical not continuous values gantt$asset.type <- factor(gantt$asset.type) # gantt chart library(ggplot2) ggplot() + geom_line(data=visitors, aes(x=time, y=count, colour=count)) + geom_rect(data=gantt, aes(xmin=dummy.start.time, xmax=dummy.end.time, ymin=-100, ymax=0, fill=asset.type)) + facet_grid(weekday~.) + scale_colour_distiller("Visitor Count", type="div", palette="RdYlGn", direction = -1) + scale_fill_manual("Asset Type", values=c("dodger blue", "black")) + theme_bw() + theme(axis.title = element_blank()) 来调整资产条的厚度。我有yminymax取负值,因此它被绘制在访客计数y范围下方。

enter image description here

我应该补充一点,你也可以使用geom_rectymax=0中制作甘特图,但是你的情节的一个特殊问题需要两种不同的色阶,而ymin只需要{{1}美学如ggplot2

以下是geom_segment代码,仅用于创建甘特图:

geom_segment

enter image description here