简体   繁体   中英

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!

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM