4

I am trying to make a plot of value vs. time (not date_time) only.

Below is a sample code for reproducibility:

date_time <- date_time <- c("2014-06-01 9:30", "2014-06-01 13:15", "2014-06-08 19:0", "2014-06-15 0:15", "2014-06-29 3:0", "2014-07-20 16:15", "2014-07-27 20:30", "2014-08-03 14:15", "2014-08-10 1:0", "2014-08-24 20:30", "2014-08-31 1:15", "2014-08-31 10:30", "2014-08-31 11:15", "2014-08-31 17:45", "2014-09-07 13:45", "2014-09-07 21:0", "2014-09-07 21:30", "2014-09-14 14:30", "2014-09-14 16:30", "2014-09-14 21:30", "2014-09-21 14:0", "2014-09-21 19:15", "2014-09-28 10:45", "2014-09-28 20:0", "2014-10-05 8:15", "2014-10-05 9:30", "2014-10-05 23:45", "2014-10-12 1:0", "2014-10-12 2:45", "2014-10-12 11:45", "2014-10-12 18:0", "2014-10-19 19:45", "2014-10-19 21:15", "2014-10-26 12:45", "2014-10-26 14:30", "2014-10-26 17:15", "2014-11-02 11:30", "2014-11-02 15:0", "2014-11-09 12:15", "2014-11-09 18:0", "2014-11-16 19:15", "2014-11-16 21:45", "2014-11-23 16:30", "2014-11-23 18:30", "2014-11-30 2:45", "2014-11-30 16:30", "2014-12-07 14:0", "2014-12-07 16:15", "2014-12-07 18:15", "2014-12-14 16:45", "2014-12-14 18:15", "2014-12-21 19:0", "2014-12-21 19:45", "2014-12-21 20:15", "2014-12-21 22:45", "2014-12-28 10:30", "2014-12-28 15:0", "2014-12-28 16:30", "2014-12-28 17:45", "2014-12-28 20:30", "2014-12-28 21:0")

value <- c(62.820, 82.044, 64.656, 70.128, 90.432, 68.292, 61.344, 95.508, 58.572, 20.678, 54.396, 56.880, 90.252, .320, 46.951, 21.102, 50.508, 63.324, 64.626, 90.396, 51.414, 11.340, 84.168, 84.708, 51.444, 87.192, 45.756, .670, 66.960, 88.096, 11.613, 76.140, 91.342, 76.733, 69.696, 23.688, 92.279, 82.141, 52.576,  3.045, 82.080, .460, 64.188, 49.536, 45.360, 62.485, 69.120, 56.592, 16.200,  0.112, 64.484, 13.675,  1.616,  1.861, 62.853, .600, 82.348, 63.873,  1.668, 64.751, 47.664)

df <- data.frame(date_time, value)

The code I used:

ggplot(df, aes(date_time, value)) + 
                    geom_point() +
                    geom_smooth() +
                    geom_hline(data = df, 
                               aes(yintercept = mean(value))) +
                    theme(axis.text.x = element_text(angle = 45, hjust = 1))

I've tried to use one the following to eliminate the date from the labels:

scale_x_discrete(labels = date_format("%Y-%m"))
scale_x_date(labels = date_format("%Y-%m"))
scale_x_datetime(format="%H:%M")

but they give me the following errors, respectively:

Error in format.default(x, format) : invalid 'trim' argument
Error: Invalid input: date_trans works with objects of class Date only
Error: Invalid input: date_trans works with objects of class Date only
3
  • 2
    In your example, date_time in df is a factor. Make it of type POSIXct and scale_x_date* should work: df <- data.frame(date_time = as.POSIXct(date_time), value).
    – lukeA
    Commented Feb 19, 2015 at 21:30
  • @lukeA Thanks for the reminder. I resorted to using abbreviated months + year instead. I couldn't fix it for time but I am still interested in doing it with time. Moreover, I had a for loop that loops over several variables and saves a (faceted) plot for every graph. It breaks with an error "Error in prettyDate....range too small for 'min.n'" Any idea why?
    – user10853
    Commented Feb 19, 2015 at 23:17
  • Because the range of x or y values is too small for the minimum number of tick marks (see ?pretty)? Hard to say, maybe some subsets have only one value...
    – lukeA
    Commented Feb 19, 2015 at 23:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.