Search code examples
rggplot2plotgeom-text

ggplot2: geom_text How to plot the time in the bars?


I have a chart which plots the time I go to sleep and the hour I awake.

The problem is for plot with geom_text the time in the chart, I can't, and I don´t know why.

This is the .csv csv

And this is my code:

library(tidyverse)

prueba <- read.csv("C:/Users/......csv", fileEncoding = "UTF-8", header = TRUE, sep = ";")


### For read the , ###
prueba$airt = as.numeric(gsub(",","\\.",prueba$airt))
prueba$airtmax = as.numeric(gsub(",","\\.",prueba$airtmax))
#prueba$airtmax1 = as.Date(gsub(":":"\\.",prueba$airtmax1))
#prueba$airtmax1 = as.character(gsub(",","\\.",prueba$airtmax1))

prueba$airtmax1 <- as.numeric(prueba$airtmax1,
                              "%H-%M-%S")

I don´t know how to read the Time format for work with

ggplot(prueba, aes(x=date)) + 
  
  #scale_x_datetime(breaks = date_breaks("1 day")) +
  scale_y_continuous(limits = c(-10,48), breaks=seq(-10,48,1), 
                     labels=str_pad(seq(-10,48,1) %% 24, 2, pad="0")) +
  ##Lines which intercept the chart
  geom_hline(yintercept=seq(0,48,24)) +
  
  geom_text(data=prueba,aes(y=airtmax1,label=airtmax1),vjust=-1)+

If you write y=airtmax,label=airtmax instead, it works good

  geom_linerange(aes(ymin = airt , ymax = airtmax  ), color = "#63C300",size = 5) + 
  coord_flip() + ylab("Tiempo en horas") + 
  ggtitle("Horas de Sueño")

Solution

  • Here is a solution it was a matter of getting the data into the proper format and ensuring the geom_text() call was plotting the correct values.

    prueba <-structure(list(date = c("21/05/20", "22/05/20", "23/05/20", "24/05/20", 
    "25/05/20", "26/05/20", "27/05/20", "28/05/20", "29/05/20", "30/05/20", 
    "31/05/20", "01/06/20", "02/06/20", "03/06/20", "04/06/20", "05/06/20", 
    "06/06/20", "07/06/20", "08/06/20", "09/6/20", "10/06/20", "11/06/20", 
    "12/06/20", "13/06/20"), airt1 = c("18:00:00", "01:00:00", "01:30:00", 
    "20:30:00", "00.00", "01:00:00", "01:00:00", "00.00", "01:00:00", 
    "01:00:00", "02:00:00", "01:00:00", "01:00:00", "01:15:00", "01:45:00", 
    "01:00:00", "01:15:00", "01:30:00", "02:00:00", "01:30:00", "20:30:00", 
    "01:00:00", "01:00:00", "02:00:00"), airtmax1 = c("08:00:00", 
    "06:00:00", "08:30:00", "07:00:00", "08:30:00", "09:15:00", "09:15:00", 
    "08:45:00", "09:00:00", "10:00:00", "09:30:00", "08:45:00", "08:03:00", 
    "08:30:00", "09:30:00", "09:23:00", "09:00:00", "09:30:00", "10:30:00", 
    "10:40:00", "11:00:00", "08:00:00", "10:45:00", "10:40:00"), 
        airt = c("18", "1", "1", "-2.5", "0", "1", "1", "0", "1", 
        "1", "2", "1", "1", "1,25", "1,7", "1", "1,25", "1,5", "2", 
        "1,5", "2,5", "1", "1", "2"), airtmax = c("32", "6", "5", 
        "7", "8", "9", "9", "8", "9", "10", "9,5", "8,75", "8,5", 
        "8,5", "9,5", "9,5", "9", "9,5", "10,5", "10,7", "11", "8", 
        "10,75", "10,75")), row.names = c(NA, -24L), class = "data.frame")
    
    library(stringr)
    #get data into proper format
    prueba$airt = as.numeric(gsub(",","\\.",prueba$airt))
    prueba$airtmax = as.numeric(gsub(",","\\.",prueba$airtmax))
    #Convert to date time objects
    prueba$airtmax1 <- as.POSIXct(prueba$airtmax1, "%H:%M:%S", tz="GMT")
    prueba$date<-as.Date(prueba$date, "%d/%m/%y")
    
    #verify data is in the correct format
    str(prueba)
    
    ggplot(prueba, aes(x=date)) + 
       scale_y_continuous(limits = c(-10,48), breaks=seq(-10,48,1), 
                          labels=str_pad(seq(-10,48,1) %% 24, 2, pad="0")) +
       ##Lines wich intercept the chart
       geom_hline(yintercept=seq(0,48,24)) +
       #plot at end time and format the airtmax1 as hour:minute
       geom_text(data=prueba, aes(y=airtmax, label=format(airtmax1, "%H:%M"), vjust=-1))+
       geom_linerange(aes(ymin = airt , ymax = airtmax  ), color = "#63C300",size = 5) + 
       coord_flip() + ylab("Tiempo en horas") + 
       ggtitle("Horas de Sueño")
    

    enter image description here

    Note: When the plot was scaled for pasting here, some labels were distorted and lost in the process