Set the height of the graphs y-axis in grid.arrange, but not of the entire plot area

后端 未结 2 1700
醉话见心
醉话见心 2021-01-18 05:49

When using grid arrange I encountered the following problem : \"inital I want all my panels (a,b,c) to

相关标签:
2条回答
  • 2021-01-18 05:54

    One hack could be to use facet_grid with scales = "free_y", and add some "invisible" data that will be used when ggplot automatically generates the y axis limits. This would work if you want to expand the automatically generated axes beyond the limits of your data, but not if you want to constrain them to exclude a portion of your data:

    data <- data.frame(values=c(runif(20,0,40),runif(40,0,15)),
               product=c(rep("a",20),rep("b",20),rep("c",20)),
               treat=c("e","f","g","h","i"),
               time=c(0,6,24,48))
    
    ggplot(data, aes(time, values, color=treat)) + 
          geom_line() + geom_point() +
          geom_hline(data=data.frame(product=c("a", "b", "c"), values=c(40, 15, 15)), 
                 aes(yintercept=values), color="white") +
          facet_grid(product~., scales="free") +
          theme(panel.background=element_blank(), panel.grid=element_blank())
    

    enter image description here

    0 讨论(0)
  • 2021-01-18 06:05

    You should use rbind instead of grid.arrange,

    plots <- list(p_a, p_b, p_c)
    grobs <- lapply(plots, ggplotGrob)
    # for gridExtra < v2.3, use do.call(gridExtra::rbind.gtable, grobs)
    # for gridExtra >= v2.3 use:
    g <- do.call(gridExtra::gtable_rbind, grobs)
    
    library(grid)
    grid.newpage()
    grid.draw(g)
    
    0 讨论(0)
提交回复
热议问题