Left align two graph edges (ggplot)

前端 未结 9 1286
梦谈多话
梦谈多话 2020-11-22 01:08

I\'m using ggplot and have two graphs that I want to display on top of each other. I used grid.arrange from gridExtra to stack them. The problem is I want the

9条回答
  •  灰色年华
    2020-11-22 01:26

    Here is another possible solution using melt from the reshape2 package, and facet_wrap:

    library(ggplot2)
    library(reshape2)
    
    dat = CO2[, c(1, 2)]
    dat$id = seq(nrow(dat))
    mdat = melt(dat, id.vars="id")
    
    head(mdat)
    #   id variable value
    # 1  1    Plant   Qn1
    # 2  2    Plant   Qn1
    # 3  3    Plant   Qn1
    # 4  4    Plant   Qn1
    # 5  5    Plant   Qn1
    # 6  6    Plant   Qn1
    
    plot_1 = ggplot(mdat, aes(x=value)) + 
             geom_bar() + 
             coord_flip() +
             facet_wrap(~ variable, nrow=2, scales="free", drop=TRUE)
    
    ggsave(plot=plot_1, filename="plot_1.png", height=4, width=6)
    

    enter image description here

提交回复
热议问题