我正在尝试绘制不同天数的访客数。我想使用facet_grid在公共X轴上的绘图彼此直接位于下方。每次尝试时,第二个图(第2天)都会在右侧结束。有人知道我做错了吗?下面是我正在使用的代码:
ggplot(count_visitors, aes(x = date)) +
geom_line(aes(y=average_count), colour=colour[1], size = 0.5) +
geom_line(aes(y=count_max), colour=colour[1], size = 0.5, alpha="0.2") +
geom_line(aes(y=count_min), colour=colour[1], size = 0.5, alpha="0.2") +
geom_ribbon(aes(ymin=count_min,ymax=count_max), fill=colour[1], alpha="0.2") +
labs(x = "Time", y = "Visitors Count") +
scale_y_continuous(breaks = seq(0, 600, by=100), limits = c(0, 600)) +
scale_x_datetime(labels = date_format("%H:%M")) +
facet_grid(day_month ~ .)
这是数据的样子:
$ date : POSIXct, format: "2017-12-02 07:00:00" "2017-12-02 07:15:00" "2017-12-02 07:30:00" "2017-12-02 07:45:00" ...
$ day_month : int 2 2 2 2 2 2 2 2 2 2 ...
$ average_count: num 1 2 2.5 3.5 9 11 19.5 31.5 62 90.5 .
$ count_min : num 0 0 0 0 2 4 9 15 39 61 ...
$ count_max : num 2 4 5 7 16 18 30 48 85 120 ...
structure(list(date = structure(c(1512198000, 1512198900, 1512199800,
1512200700, 1512201600, 1512202500, 1512203400, 1512204300, 1512205200,
1512206100, 1512207000, 1512207900, 1512208800, 1512209700, 1512210600,
1512211500, 1512212400, 1512213300, 1512214200, 1512215100, 1512216000,
1512216900, 1512217800, 1512218700, 1512219600, 1512220500, 1512221400,
1512222300, 1512223200, 1512224100, 1512225000, 1512225900, 1512226800,
1512227700, 1512228600, 1512229500, 1512230400, 1512231300, 1512232200,
1512233100, 1512234000, 1512234900, 1512235800, 1512236700, 1512237600,
1512238500, 1512239400, 1512240300, 1512241200, 1512242100, 1512243000,
1512243900, 1512244800, 1512245700, 1512246600, 1512247500, 1512248400,
1512249300, 1512250200, 1512251100, 1512252000, 1512252900, 1512253800,
1512254700, 1512255600, 1512111600, 1512112500, 1512113400, 1512114300,
1512115200, 1512116100, 1512117000, 1512117900, 1512118800, 1512119700,
1512120600, 1512121500, 1512122400, 1512123300, 1512124200, 1512125100,
1512126000, 1512126900, 1512127800, 1512128700, 1512129600, 1512130500,
1512131400, 1512132300, 1512133200, 1512134100, 1512135000, 1512135900,
1512136800, 1512137700, 1512138600, 1512139500, 1512140400, 1512141300,
1512142200, 1512143100, 1512144000, 1512144900, 1512145800, 1512146700,
1512147600, 1512148500, 1512149400, 1512150300, 1512151200, 1512152100,
1512153000, 1512153900, 1512154800, 1512155700, 1512156600, 1512157500,
1512158400, 1512159300, 1512160200, 1512161100, 1512162000, 1512162900,
1512163800, 1512164700, 1512165600, 1512166500, 1512167400, 1512168300,
1512169200), class = c("POSIXct", "POSIXt"), tzone = "GMT"),
day_month = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 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, 1L, 1L, 1L, 1L, 1L),
count_min = c(0, 0, 0, 0, 2, 4, 9, 15, 39, 61, 78, 95, 110,
121, 135, 151, 154, 173, 175, 187, 201, 227, 239, 254, 265,
275, 278, 288, 293, 290, 290, 293, 295, 299, 297, 284, 279,
278, 255, 250, 231, 224, 204, 184, 178, 170, 161, 149, 146,
148, 153, 150, 138, 127, 119, 112, 91, 79, 53, 40, 29, 15,
11, 9, 5, 1, 1, 1, 1, 1, 2, 3, 5, 14, 16, 26, 35, 58, 67,
89, 114, 141, 159, 183, 187, 198, 208, 207, 206, 209, 209,
204, 194, 180, 175, 156, 142, 145, 133, 128, 121, 104, 100,
85, 74, 75, 81, 93, 106, 104, 116, 121, 137, 151, 153, 159,
168, 165, 159, 156, 144, 119, 102, 84, 60, 35, 23, 17, 15,
10), count_max = c(2, 4, 5, 7, 16, 18, 30, 48, 85, 120, 146,
176, 207, 229, 253, 295, 312, 327, 348, 370, 392, 418, 446,
457, 489, 501, 509, 507, 514, 515, 533, 550, 564, 554, 557,
552, 552, 524, 502, 476, 447, 432, 411, 400, 380, 352, 341,
322, 314, 312, 303, 292, 288, 262, 239, 219, 202, 177, 138,
108, 81, 43, 32, 22, 12, 2, 2, 2, 2, 2, 7, 10, 21, 33, 44,
64, 89, 117, 153, 186, 222, 260, 279, 298, 323, 332, 341,
345, 349, 361, 361, 367, 364, 352, 324, 309, 291, 282, 267,
256, 240, 220, 197, 192, 185, 181, 184, 195, 203, 208, 202,
218, 245, 269, 297, 312, 320, 315, 317, 301, 284, 250, 220,
194, 166, 124, 77, 41, 30, 20), average_count = c(1, 2, 2.5,
3.5, 9, 11, 19.5, 31.5, 62, 90.5, 112, 135.5, 158.5, 175,
194, 223, 233, 250, 261.5, 278.5, 296.5, 322.5, 342.5, 355.5,
377, 388, 393.5, 397.5, 403.5, 402.5, 411.5, 421.5, 429.5,
426.5, 427, 418, 415.5, 401, 378.5, 363, 339, 328, 307.5,
292, 279, 261, 251, 235.5, 230, 230, 228, 221, 213, 194.5,
179, 165.5, 146.5, 128, 95.5, 74, 55, 29, 21.5, 15.5, 8.5,
1.5, 1.5, 1.5, 1.5, 1.5, 4.5, 6.5, 13, 23.5, 30, 45, 62,
87.5, 110, 137.5, 168, 200.5, 219, 240.5, 255, 265, 274.5,
276, 277.5, 285, 285, 285.5, 279, 266, 249.5, 232.5, 216.5,
213.5, 200, 192, 180.5, 162, 148.5, 138.5, 129.5, 128, 132.5,
144, 154.5, 156, 159, 169.5, 191, 210, 225, 235.5, 244, 240,
238, 228.5, 214, 184.5, 161, 139, 113, 79.5, 50, 29, 22.5,
15)), class = "data.frame", row.names = c(NA, -130L))
答案 0 :(得分:0)
一种选择是改为使用facet_wrap
。
请注意,我删除了许多您相当多余的代码(针对该问题)。建议您看看how to create an MCVE
ggplot(count_visitors, aes(x = date)) +
geom_line(aes(y=average_count), size = 0.5) +
geom_ribbon(aes(ymin=count_min,ymax=count_max), alpha="0.2") +
facet_wrap(day_month ~ ., nrow = 2, scales = 'free_x')