#r #dataframe
Вопрос:
Я создал приведенный ниже ggplot, но я не знаю, как выделить и добавить 5 geom_vline() в мой участок по адресу: 04:00:00, 08:00:00,12:00:00,16:00:00 а 20:00:00? Есть ли способ сделать это? Также как добавить время к оси x, как я пытался с x_continous, но это не работает?
Пример кода
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"))
Ответ №1:
Вы можете добавить свои 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))
Комментарии:
1. большое спасибо за ваше время. Я не знаю, как выделить по оси x»04:00:00″, «08:00:00″,»12:00:00″,»16:00:00», «20:00:00», это не работает с x_continous. Alos почему белый промежуток между осями x и y?
2. Попробуйте с
scale_x_time(breaks = lubridate::hms(c("04:00:00", "08:00:00","12:00:00","16:00:00", "20:00:00")))
3. Я попробовал scale_x_continous (), и это не сработало
4. есть ли способ расположить дату в порядке возрастания, а не в порядке убывания?
5. Поскольку ваши даты-это персонажи, с которыми вы могли бы попробовать
fct_rev(variable)
. Это будет работать для ваших данных примера. Кстати: Вы могли бы удалить пробелы с помощьюscale_x_time(..., expand=c(0,0))
иscale_y_discrete(..., expand = ...)
. Это приведет к удалению расширения масштаба по умолчанию, добавленного ggplot2.