简体   繁体   中英

Issue with date to plot box plot with ggplot

I have the following data frame d :

             TS Turbidity
1 2014-12-12 00:00:00        87
2 2014-12-12 00:15:00        87
3 2014-12-12 00:30:00        91
4 2014-12-12 00:45:00        84
5 2014-12-12 01:00:00        92
6 2014-12-12 01:15:00        89

TS is my time combining the year, month, day, hour, minutes, and second. When I look at the nature of my data, TS is:

$ TS       : POSIXct, format: "2014-12-12 00:00:00" "2014-12-12   00:15:00" 

So for me , R understand that TS is date format.

I want to create boxplot per month (I precise that I have several years of data). I create a new column Month as follow:

d$Month<-as.Date(cut(d$TS, breaks="month"))

Then I plot this function:

ggplot(d, aes(x = factor(Month), y = Turbidity))+ geom_boxplot() + theme_bw()

This function plots well my data but I have too many x-labels and would like to plot labels for every 4 months for example. I add scale_x_date:

ggplot(d, aes(x = factor(Month), y = Turbidity))+ geom_boxplot() + theme_bw() + 
scale_x_date(date_breaks = "4 month", date_labels = "%B")

It is at this step that I have trouble. I got this error message :

" Error: Invalid input: date_trans works with objects of class Date only". 

But R precise that Month is in a date format.

$ Month    : Date, format: "2014-12-01" "2014-12-01" "2014-12-01"

I look at forums but I cannot figure out where is the problem because for me I have already state that Month was a date.

Thanks for any help!

In your call to ggplot you explicitly convert Month to a factor with aes(x = factor(Month)) internally. Try removing the factor() wrapper from Month .

This doesn't change the object outside of ggplot, which is why you still see that it's class is Date when you check it. But you are definitely converting the class from Date to Factor inside of ggplot here.

One approach could be as(with modified data):

library(ggplot2)
library(lubridate)

df %>% mutate(TS = ymd_hms(TS)) %>%
  ggplot(aes(x = cut(TS, breaks="quarter"), y = Turbidity)) +    
  geom_boxplot() + 
  labs(x = "Start date of Quarter") +
  theme_bw()

在此处输入图片说明

Data : Different from OP

df <- read.table(text = 
"TS                           Turbidity
'2014-09-12 00:00:00'        87
'2014-09-12 00:15:00'        107
'2014-10-12 00:30:00'        91
'2014-10-12 00:30:00'        50
'2014-11-12 00:45:00'        84
'2014-11-12 00:45:00'        60
'2014-12-12 01:00:00'        92
'2014-12-12 01:15:00'        60
'2015-01-12 00:00:00'        87
'2015-01-12 00:15:00'        107
'2015-02-12 00:30:00'        91
'2015-02-12 00:30:00'        50
'2015-03-12 00:45:00'        84
'2015-03-12 00:45:00'        60
'2015-04-12 01:00:00'        92
'2015-04-12 01:15:00'        60
'2015-05-12 00:00:00'        87
'2015-05-12 00:15:00'        107
'2015-06-12 00:30:00'        91
'2015-06-12 00:30:00'        50
'2015-07-12 00:45:00'        84
'2015-07-12 00:45:00'        60
'2015-08-12 01:00:00'        92
'2015-08-12 01:15:00'        60", header = TRUE, stringsAsFactors = FALSE)

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