[英]ggplot2: geom_vline to heatmap
我创建了以下ggplot,但我不知道如何突出,并在我的剧情在加5 geom_vline()04:00:00,08:00:00,12:00:00,16:00:00和20:00:00 ? 有没有办法做到这一点? 另外如何像我尝试使用 x_continous 那样将时间添加到 x 轴,但它不起作用?
示例代码
ggplot(df, aes(x=time, y=variable, fill=value)) +
geom_tile() +
scale_fill_gradient(low="lightyellow", high="red") +
labs(x="Time", y="Date", title="", fill="") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text( hjust = 1), face="bold", size=16, color="black") +
theme(axis.text.y = element_text( hjust = 1), face="bold", size=16, color="black") +
theme_bw()+
theme(plot.title = element_text(hjust = 0.5))
样本数据:仅选择前 300 个
structure(list(time = structure(c(1800, 3600, 5400, 7200, 9000,
10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200,
27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400,
43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600,
59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800,
75600, 77400, 79200, 81000, 82800, 84600, 86400, 1800, 3600,
5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600,
23400, 25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800,
39600, 41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000,
55800, 57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200,
72000, 73800, 75600, 77400, 79200, 81000, 82800, 84600, 86400,
1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000,
19800, 21600, 23400, 25200, 27000, 28800, 30600, 32400, 34200,
36000, 37800, 39600, 41400, 43200, 45000, 46800, 48600, 50400,
52200, 54000, 55800, 57600, 59400, 61200, 63000, 64800, 66600,
68400, 70200, 72000, 73800, 75600, 77400, 79200, 81000, 82800,
84600, 86400, 1800, 3600, 5400, 7200, 9000, 10800, 12600, 14400,
16200, 18000, 19800, 21600, 23400, 25200, 27000, 28800, 30600,
32400, 34200, 36000, 37800, 39600, 41400, 43200, 45000, 46800,
48600, 50400, 52200, 54000, 55800, 57600, 59400, 61200, 63000,
64800, 66600, 68400, 70200, 72000, 73800, 75600, 77400, 79200,
81000, 82800, 84600, 86400, 1800, 3600, 5400, 7200, 9000, 10800,
12600, 14400, 16200, 18000, 19800, 21600, 23400, 25200, 27000,
28800, 30600, 32400, 34200, 36000, 37800, 39600, 41400, 43200,
45000, 46800, 48600, 50400, 52200, 54000, 55800, 57600, 59400,
61200, 63000, 64800, 66600, 68400, 70200, 72000, 73800, 75600,
77400, 79200, 81000, 82800, 84600, 86400, 1800, 3600, 5400, 7200,
9000, 10800, 12600, 14400, 16200, 18000, 19800, 21600, 23400,
25200, 27000, 28800, 30600, 32400, 34200, 36000, 37800, 39600,
41400, 43200, 45000, 46800, 48600, 50400, 52200, 54000, 55800,
57600, 59400, 61200, 63000, 64800, 66600, 68400, 70200, 72000,
73800, 75600, 77400, 79200, 81000, 82800, 84600, 86400, 1800,
3600, 5400, 7200, 9000, 10800, 12600, 14400, 16200, 18000, 19800,
21600), class = c("hms", "difftime"), units = "secs"), variable = c("02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019", "02/01/2019",
"02/01/2019", "02/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019", "03/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019", "04/01/2019",
"04/01/2019", "04/01/2019", "04/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019", "05/01/2019",
"05/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019",
"06/01/2019", "06/01/2019", "06/01/2019", "06/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019", "07/01/2019",
"07/01/2019", "07/01/2019", "08/01/2019", "08/01/2019", "08/01/2019",
"08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019",
"08/01/2019", "08/01/2019", "08/01/2019", "08/01/2019"), value = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), row.names = c(NA, -300L), class = c("tbl_df",
"tbl", "data.frame"))
您可以添加您的geom_vline
由次向量转换为s hms
使用格式lubridate::hms
并将结果传递给xintercept
的说法geom_vline
:
library(ggplot2)
library(lubridate)
ggplot(df, aes(x=time, y=variable, fill=value)) +
geom_tile() +
geom_vline(xintercept = lubridate::hms(c("04:00:00", "08:00:00","12:00:00","16:00:00", "20:00:00"))) +
scale_fill_gradient(low="lightyellow", high="red") +
labs(x="Time", y="Date", title="", fill="") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text( hjust = 1), face="bold", size=16, color="black") +
theme(axis.text.y = element_text( hjust = 1), face="bold", size=16, color="black") +
theme_bw()+
theme(plot.title = element_text(hjust = 0.5))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.