Stratix
Stratix

Reputation: 191

R - Plotting 2 different sized variables with different time axis with ggplot

I have two variables that are different sizes. These variables were measured on very similar time scales, but they are different by a few days (data spans about half a year). Here are the variables, along with the time axes:

data1 # double, length of 229081
time1 # double, length of 229081
data2 # double, length of 230842
time2 # double, length of 230842

I want to plot these two variables as line plots on the same graph using ggplot. I've tried the following but to no avail:

data = data.frame(data1, data2) 
qplot(time2, data, color=colors, scale_colour_identity(guide="legend"),
          geom=c("line", "line"))

I feel like the above should work, since I'm using the bigger time axis, which encompasses the smaller data set. I tried looking for a similar question on stackoverflow, but couldn't quite find it.

Upvotes: 1

Views: 1864

Answers (1)

PavoDive
PavoDive

Reputation: 6496

You will need to pass an independent data frame to an additional call to geom_line:

As you didn't provide any reproducible example (it's good to do so!):

data1<-data.frame(d1=runif(100),t1=sample(1:1000,100))
data2<-data.frame(d1=runif(98),t1=sample(1:2000,98))

Then you can plot:

ggplot(data1,aes(d1,t1))+geom_line()+geom_line(data=data2,aes(d1,t1,color="red"))

Upvotes: 2

Related Questions