2
votes

I have two datasets like the following and I'm trying to plot them in one figure using either ggplot2 or regular plot:

df1
date                count
2014-09-10 07:14:00 0.07253886
2014-09-10 07:19:00 0.03626943
2014-09-10 07:24:00  0.07253886
2014-09-10 07:29:00  0.19689119
2014-09-10 07:34:00  0.13989637
2014-09-10 07:39:00  0.88601036

df2
date                count
2014-09-10 11:25:00  1.00000000
2014-09-10 11:30:00  0.47058824
2014-09-10 11:35:00  0.00000000
2014-09-10 11:40:00  0.11764706
2014-09-10 11:45:00  0.00000000
2014-09-10 11:50:00  0.05882353

As you can see the data frames have different date values (so you can't simply merge the two data frames by date column), although they are separated with 5 minutes interval. All I'm trying to do is to have one plot that starts from the minimum date and ranges to maximum date of these two data frames, while plot the click counts correctly for each time interval.

1
Just rbind them together - rbind(df1,df2) - then you should be able to plot away fine. - thelatemail

1 Answers

2
votes

You can create a new column "type" for both df1 & df2 then bind them together as in @thelatemail 's comment

> df <- rbind(df1, df2)
> dput(df)
    structure(list(Date = structure(c(1410358440, 1410358740, 1410359040, 
    1410359340, 1410359640, 1410359940, 1410373500, 1410373800, 1410374100, 
    1410374400, 1410374700, 1410375000), tzone = "", class = c("POSIXct", 
    "POSIXt")), count = c(0.07253886, 0.03626943, 0.07253886, 0.19689119, 
    0.13989637, 0.88601036, 1, 0.47058824, 0, 0.11764706, 0, 0.05882353
    ), type = c("df1", "df1", "df1", "df1", "df1", "df1", "df2", 
    "df2", "df2", "df2", "df2", "df2")), .Names = c("Date", "count", 
    "type"), row.names = c(NA, 12L), class = "data.frame")

Now it's relatively easy to plot using ggplot

library(ggplot2)
ggplot(df, aes(x = Date, y = count, color = type)) + geom_line() + theme_bw()

enter image description here