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

前端 未结 3 540
刺人心
刺人心 2020-11-27 05:03

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

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 05:35

    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

提交回复
热议问题