0

I need help because I am unfamiliar with using ggplot2 to plot two time series datasets with different number of rows and different measurements. I found in previous answers regarding how to solve the first problem and I'm pretty sure I can solve also the second one, but I don't know how to solve them together. Here is the code to simulate what I need to do, but in the plot that the code produces I need to solve the problem with the different ranges. How can I set two different y-axes?

x <- rnorm(100)
y <- rnorm(100)
data1 <- data.frame(x,y)
x1 <- rnorm(50)
y1 <-rnorm(50) + 500
data2 <- data.frame(x1,y1)
names(data2)[1]<-paste("x")
names(data2)[2]<-paste("y")
data <- rbind(data1,data2)
data$dataset = c(rep("A", 100), rep("B", 50))
ggplot(data, aes(x = x, y = y, col=dataset)) + geom_line()

Thanks for your attention and excuse me if the question is not clear enough, this is my first question here and I know I have to learn a lot.

David C.
  • 1,974
  • 2
  • 19
  • 29
V. Ang
  • 65
  • 7

1 Answers1

0

I would use facet_grid() with free y axis

ggplot(data, aes(x = x, y = y, col=dataset)) + geom_line() + 
facet_grid(dataset ~., scales = "free_y")
Hao
  • 7,476
  • 1
  • 38
  • 59
  • `scales = "free` seem to work just fine in this case. See this post: http://stackoverflow.com/questions/18046051/setting-individual-axis-limits-with-facet-wrap-and-scales-free-in-ggplot2 – David C. Jan 28 '17 at 21:39