1

I have three data frames df1, df2, df3 in the following format

datetime               value
2012-01-02 00:00:00    1.2
2012-01-02 00:15:00    1.7
2012-01-02 00:30:00    0.5
2012-01-02 00:45:00    0.8


datetime               value
2012-01-03 00:00:00    0.4
2012-01-03 00:15:00    1.1
2012-01-03 00:30:00    1.8
2012-01-03 00:45:00    0.7

datetime               value
2012-01-04 00:00:00    2.9
2012-01-04 00:15:00    0.7
2012-01-04 00:30:00    1.3
2012-01-04 00:45:00    0.3

All three data frames have different dates, the datetime is in POSIXlt format.

Since, time index are same, I want to plot all the lines on the same plot. But I am not sure how to extract time only. I have done following so far:

library(chron)
timeOnly = times(format(df1$datetime, "%H:%M:%S"))
plot(timeOnly, df1$value)

But the x-axis tick labels go from 0 to 1, instead of 00:00:00 to 23:45:00

4
  • I answered without exactly the same data, but I think is the answer you are looking for Commented Dec 20, 2016 at 15:14
  • @chintans I noticed a typo in your last line. You defined the times you want as "timeOnly1" but then you put "timeOnly" in the plot function. Perhaps you defined timeOnly differently than timeOnly1? I ran the same code but with plot(timeOnly1, df1$value) and got the correct x-axis. Commented Dec 20, 2016 at 15:16
  • Thanks, I have fixed the typo. But I want the x-axis to be 00:00 to 23:45. How to get that? Commented Dec 20, 2016 at 15:20
  • Wow this is surprisingly tough. The axis wants to autoformat to 0-1 once you include the whole range of times. Well, it seems like you have 96 observations per vector. Once crude approach would be to limit the original plot and then add on the extras like this: plot(x[1:93], y[1:93]); points(x, y). This obviously won't look perfect though Commented Dec 20, 2016 at 15:46

1 Answer 1

2

Edited to have different dates, This is how I would solve it, add a class, and then rbind the dataframes and plot it:

library(lubridate)
set.seed(1)
df1 <- data.frame(date_time = seq(ymd_hm('2012-01-02 00:00'),ymd_hm('2012-01-02 23:45'), by = '15 min')
              , value = rnorm(4, mean = 1, sd =0.2), class = "a")
set.seed(2)
df2 <- data.frame(date_time = seq(ymd_hm('2012-01-03 00:00'),ymd_hm('2012-01-03 23:45'), by = '15 min')
              , value = rnorm(4, mean = 1, sd =0.2), class = "b")
set.seed(3)
df3 <- data.frame(date_time = seq(ymd_hm('2012-01-04 00:00'),ymd_hm('2012-01-04 23:45'), by = '15 min')
              , value = rnorm(4, mean = 1, sd =0.2), class = "c")


df <- rbind(df1,df2, df3)
df$time <- as_date(hm(paste(hour(df$date_time),minute(df$date_time), sep=":")))
library(ggplot2)

ggplot(df, aes(x = time, y = value)) + geom_line(aes(color = class))+ scale_x_datetime(date_labels = "%H %M")

enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks, but you have kept same date in all data frames. In my case, all three data frames have different dates.
sorry@chintans, I see what you need, be back in a second
ready @chintans I fixed it, you use different dates and makes the same plot, look how I changed the dates of the data frame and still works
Thank you, this seems to work, except the following problem: In my plot, I get xtick labels as Jan 01 00:00, Jan 01 06:00, Jan 01 12:00, Jan 01 18:00, Jan 02 00:00. I would like the xtick labels to be what you have. But when I copy and paste your code, I get the same same result as you have.
@chintans changed it to 23:45. by the way, in case you don't know in r you can use the dput() function to replicate your data, if you dont want to give it all away you can use dput(head(yourdata)).
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.