Home > OS >  ggplot2: geom_vline to heatmap
ggplot2: geom_vline to heatmap

Time:10-24

I created the below ggplot but I don't know how to highlight and to add 5 geom_vline() in my plot at: 04:00:00, 08:00:00,12:00:00,16:00:00 and 20:00:00? Is a way to do that? Also how to add the time to the x axis as I tried with x_continous but it does not work?

enter image description here

Sample code

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))

Sample data: Selected only the first 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"))

CodePudding user response:

You could add your geom_vlines by converting the vector of times to hms format using lubridate::hms and passing the result to the xintercept argument of 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))

  • Related