Search code examples
rggplot2boxplotdifftime

Using 'difftime' data in a ggplot2 boxplot in R


I created a difftime object to determine the amount of hours it takes to report a crime that has occurred. Also, in the same dataset I have a variable which indicates whether the crime occurred on a weekday or in the weekend. Now I'd like to create a ggplot2 boxplot with 'weekday' and 'weekend' on the x-axis and use difftime on the y-axis.

I used: ggplot(data = data, aes(x = workday, y = difftime_var)) + geom_boxplot()

However, this gives the warning: Don't know how to automatically pick scale for object of type difftime. Defaulting to continuous.

I'd like to adjust the boxplot in such way that it looks like a 'real' boxplot, showing the mean amount of time it takes etc. Right now, it's basically a flat line at the bottom of the graph with a few dots above. The y-axis goes from 0 to 40 000. Probably because the min and max value of the difftime object are very small / large.

Thanks in advance for helping out!


Solution

  • Please provide an reproducible example dataset to your question.

    I guess the problem is that difftime has a huge range, which makes it impossible to show a boxplot. First thing you can try is

    ggplot(data = data, aes(x = workday, y = difftime_var)) +       
    geom_boxplot(outlier.shape=NA)
    

    Another (not elegant) way is to set a limit to the yaxis:

    ggplot(data = data, aes(x = workday, y = difftime_var)) +       
    geom_boxplot() + ylim(ymin, ymax)
    

    For more information, there was a similar question asked before: How to remove outliers in boxplot in R?