Alex Rivera | Logout

Plot 4 curves in a single plot with 3 y-axes

Asked 2011-03-29T22:46:47.313
26

I have 4 sets of values: y1, y2, y3, y4 and one set x. The y values are of different ranges, and I need to plot them as separate curves with separate sets of values on the y-axis.

To put it simple, I need 3 y-axes with different values (scales) for plotting on the same figure.

Edit
Report

1 Answer

22

If you want to go down the path of learning a plotting package beyond the base graphics, here's a solution with ggplot2 using the variables from @Rguy's answer:

library(ggplot2)
dat <- data.frame(x, y1, y2, y3, y4)

dat.m <- melt(dat, "x")

ggplot(dat.m, aes(x, value, colour = variable)) + geom_line() +
facet_wrap(~ variable, ncol = 1, scales = "free_y") +
scale_colour_discrete(legend = FALSE)

enter image description here

answered 2011-03-30T02:21:05.993

Your Answer