PhiSeu PhiSeu - 2 months ago 4x
R Question

r: How to print a histogram of daytimes with times in Format e.g. 13:00 in ggvis

I have a data.frame with datetime objects, read in from a csv file and turned into datetime Objects with

from the lubridate package. I want to print a histogram with the distribution of the times, independent of the date. Here is an example, which already looks pretty good:


# Create Example
GESPEICHERT <- c(rep("01/05/2016 15:26",times=10),
rep("13/05/2016 00:17",times=5),
rep("01/08/2016 12:05",times=10),
rep("17/04/1983 23:39",times=5),
rep("06/07/2015 09:36",times=10))
INFO <- c(rep("Bla",times=30),rep("Bla Bla",times=10))

df_time <-,INFO,stringsAsFactors=FALSE)

df_time %>%
mutate(GESPEICHERT = dmy_hm(GESPEICHERT)) -> df_time

# Trying to reduce datetime Object to time
df_time %>%
mutate(UHRZ_NUM = hour(GESPEICHERT) * 60 + minute(GESPEICHERT)) %>% # Time in Minute Numbers, not necessary
mutate(GESP_UHRZEIT = format(GESPEICHERT,format="%H:%M")) %>% #
mutate(GESP_UHRZEIT = as.POSIXct(GESP_UHRZEIT,format="%H:%M")) -> df_time

# Plot
df_time %>%
ggvis(x=~GESP_UHRZEIT) %>%
layer_histograms() %>%
scale_datetime("x", nice = "minute",label="daytime")

histogram daytimes ggvis

I have two problems here with my x-axis:

  1. I want the x-axis to show times in the form of e.g. 15:00 not 03PM

  2. At both sides it shows todays date and the date of the next day, because in my code I delete the date with
    mutate(GESP_UHRZEIT = format(GESPEICHERT,format="%H:%M"))
    so it takes todays date as date.
    How can I get rid of that? (Can I create only a time object? Can I convert it to numbers but still show it as time?)

If somebody can give me some tips it would be much appreciated. Alternative packages, changing the data structure, every idea is welcome.

Additional question:
If somebody knows how to make the binwidth interactive (in this case!), please tell me. It is not necessary, but would be a nice feature for me. I did it in cases where there are numbers, but was not successful with datetime objects.


just add add_axis with the right format code to your pipe:

> df_time %>%
     ggvis(x=~GESP_UHRZEIT) %>% 
     layer_histograms() %>% 
     scale_datetime("x", nice = "minute",label="daytime") %>%

This will give a plot with only hour:minute times, and not show any days.