Left align two graph edges (ggplot)

前端 未结 9 1280
梦谈多话
梦谈多话 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

    0 讨论(0)
  • 2020-11-22 01:31

    On http://rpubs.com/MarkusLoew/13295 is a really easy solution available (last item) Applied to this problem:

    require(ggplot2);require(gridExtra)
    A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() 
    B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 
    grid.draw(rbind(ggplotGrob(A), ggplotGrob(B), size="first"))
    

    you can also use this for both width and height:

    require(ggplot2);require(gridExtra)
    A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() 
    B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 
    C <- ggplot(CO2, aes(x=conc)) + geom_bar() +coord_flip()
    D <- ggplot(CO2, aes(x=uptake)) + geom_bar() +coord_flip() 
    grid.draw(cbind(
                rbind(ggplotGrob(A), ggplotGrob(B), size="first"),
                rbind(ggplotGrob(C), ggplotGrob(D), size="first"),
                size='first'))
    
    0 讨论(0)
  • 2020-11-22 01:31

    The egg package wraps ggplot objects into a standardised 3x3 gtable, enabling the alignment of plot panels between arbitrary ggplots, including facetted ones.

    library(egg) # devtools::install_github('baptiste/egg')
    library(ggplot2)
    
    p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
      geom_point() 
    
    p2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) +
      geom_point() + facet_wrap( ~ cyl, ncol=2, scales = "free") +
      guides(colour="none") +
      theme()
    
    ggarrange(p1, p2)
    

    0 讨论(0)
  • 2020-11-22 01:32

    I wanted to generalize this for any number of plots. Here is a step-by-step solution using the approach by Baptiste:

    plots <- list(A, B, C, D)
    grobs <- list()
    widths <- list()
    

    collect the widths for each grob of each plot

    for (i in 1:length(plots)){
        grobs[[i]] <- ggplotGrob(plots[[i]])
        widths[[i]] <- grobs[[i]]$widths[2:5]
    }
    

    use do.call to get the max width

    maxwidth <- do.call(grid::unit.pmax, widths)
    

    asign the max width to each grob

    for (i in 1:length(grobs)){
         grobs[[i]]$widths[2:5] <- as.list(maxwidth)
    }
    

    plot

    do.call("grid.arrange", c(grobs, ncol = 1))
    
    0 讨论(0)
  • 2020-11-22 01:37

    Using cowplot package:

    A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() 
    B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 
    
    library(cowplot)
    plot_grid(A, B, ncol=1, align="v")
    

    0 讨论(0)
  • 2020-11-22 01:42

    At best this is a hack:

    library(wq)
    layOut(list(A, 1, 2:16),  list(B, 2:3, 1:16))
    

    It feels really wrong though.

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