Side-by-side plots with ggplot2

后端 未结 13 2463
轮回少年
轮回少年 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:44

    Using tidyverse:

    x <- rnorm(100)
    eps <- rnorm(100,0,.2)
    df <- data.frame(x, eps) %>% 
      mutate(p1 = 3*x+eps, p2 = 2*x+eps) %>% 
      tidyr::gather("plot", "value", 3:4) %>% 
      ggplot(aes(x = x , y = value)) + 
        geom_point() + 
        geom_smooth() + 
        facet_wrap(~plot, ncol =2)
    
    df
    

    0 讨论(0)
提交回复
热议问题