Side-by-side plots with ggplot2

后端 未结 13 2630
轮回少年
轮回少年 2020-11-22 02:06

I would like to place two plots side by side using the ggplot2 package, i.e. do the equivalent of par(mfrow=c(1,2)).

For example, I would like to have t

13条回答
  •  轮回少年
    2020-11-22 02:31

    Using the reshape package you can do something like this.

    library(ggplot2)
    wide <- data.frame(x = rnorm(100), eps = rnorm(100, 0, .2))
    wide$first <- with(wide, 3 * x + eps)
    wide$second <- with(wide, 2 * x + eps)
    long <- melt(wide, id.vars = c("x", "eps"))
    ggplot(long, aes(x = x, y = value)) + geom_smooth() + geom_point() + facet_grid(.~ variable)
    

提交回复
热议问题