GithubHelp home page GithubHelp logo

Comments (11)

kendonB avatar kendonB commented on July 30, 2024 42

Worked it out. For future googlers, plot_grid doesn't add space; it simply uses the spacing already in the plots, which you can remove using theme(plot.margin = unit(c(0, 0, 0, 0), "cm")).

library(ggplot2)
library(cowplot)
p1 <- qplot(1:10, 1:10) + theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
p2 <- qplot(1:10, (1:10)^2) + theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
plot_grid(p1, p2, p1, p2, p1, p2, p1, p2, p1, p2, p1, p2)

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024 5

For those kinds of problems I would now recommend the patchwork library. It's inherently difficult with plot_grid(), due to its underlying design.

from cowplot.

eipi10 avatar eipi10 commented on July 30, 2024 4

Claus,

It seems like the approach above doesn't work when the axis labels and titles take up different amounts of space in different panels or when you want to get rid of the axis titles and labels in some panels but not others.

A reproducible example is below. It is based on the idea that sometimes you want a faceted plot, but you want to customize each facet in ways that are not possible with "regular" faceting. So, instead, you split the data frame by the desired faceting column, create a separate plot for each "facet", and then lay the plots out together with plot_grid as if they had been faceted (see, for example, this SO answer).

It would be nice if there were a way to directly control the space between panels in plot_grid, even in the case I discuss below. If this is already possible, and my examples are based on a misunderstanding, please let me know.

Thanks,
Joel

Example 1: We create separate plots. Then we remove the y-axis titles, labels, and ticks from all but the plot that will be left-most when we lay them out. We set the left margin of all but the left-most plot to -1 cm. But note in the image that even though we got rid of the left margin on five of the six panels, plot_grid seems to leave space between all the plots as if it were allowing for the width of y-axis title, labels, and ticks in the left-most plot.

library(tidyverse)
library(cowplot)

# Create list of plots
plist = split(mtcars, mtcars$carb) %>%  
  map(~ .x %>% group_by(carb) %>%
        ggplot(aes(x = mpg, y = wt)) + 
         geom_point())

# Remove y-axis labels, title, and ticks from all but the left-most plot
plist[2:length(plist)] = plist[2:length(plist)] %>% 
  map(~ .x + theme(axis.text.y=element_blank(),
                   axis.title.y=element_blank(),
                   axis.ticks.y=element_blank(),
                   plot.margin=margin(l=-1, unit="cm"))) 

plot_grid(plotlist=plist, ncol=length(plist), align="hv")

rplot15

Example 2: Same idea, but now we set the left margin of all the plots to be -1 cm. Now we get the thin margins that we want between plots, but at the expense of cutting off the title and most of the labels at the left edge of the plot.

# Create a list of plots
plist = split(mtcars, mtcars$carb) %>%  
  map(~ .x %>% group_by(carb) %>%
        ggplot(aes(x = mpg, y = wt)) + 
        geom_point() +
        theme(plot.margin=margin(l=-0.8,unit="cm")))

# Remove y-axis labels, title, and ticks from all but the left-most plot
plist[2:length(plist)] = plist[2:length(plist)] %>% 
  map(~ .x + theme(axis.text.y=element_blank(),
                   axis.title.y=element_blank(),
                   axis.ticks.y=element_blank())) 

plot_grid(plotlist=plist, ncol=length(plist), align="v")

rplot10

from cowplot.

yewero avatar yewero commented on July 30, 2024 3

@kendonB Here is a solution (by Valentin):
https://stackoverflow.com/questions/41834093/decrease-margins-between-plots-when-using-cowplot

plot5 <- plot_grid(plot1, NULL, plot2, rel_widths = c(1, -0.1, 1), align = "hv",
                   labels = c("A", "B"), nrow = 1)
plot5

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

I will need a reproducible code example to understand what the issue is.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

I'm closing this because there isn't sufficient information here to understand what the issue is.

from cowplot.

kendonB avatar kendonB commented on July 30, 2024

Apologies for missing explaining this earlier. Suppose I have a grid:

library(ggplot2)
library(cowplot)
p1 <- qplot(1:10, 1:10)
p2 <- qplot(1:10, (1:10)^2)
# simple grid
plot_grid(p1, p2)

I would like to squish them closer together. Is this possible in cowplot? The white space becomes quite large compared to the plots with denser grids.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

Yes, that's how it works. Thanks for posting a clear example solution, this question comes up quite frequently.

from cowplot.

clauswilke avatar clauswilke commented on July 30, 2024

The trick is to extract the x axis label from a plot and add it as a separate column in a plot that otherwise looks like your Example 2. Something along the lines of the last example here: https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html

The code you'd need to write to extract the axis labels would be similar to the get_legend function, you just need to extract a different grob, probably the one called ylab-l:
https://github.com/wilkelab/cowplot/blob/master/R/get_legend.R

from cowplot.

luize-bg avatar luize-bg commented on July 30, 2024

It seems to me that when we use align="hv" and the last element of the first line is NULL the approach to making the margin of each plot smaller does not work. I'm trying to approximate density plots to the top and left margin of a line with scater plots (as eipi10 commented) and let the axes of the plots allingned. But when I use the (plot.margin) command I really do not have succees in making the plots closer togheter.

from cowplot.

BrownAlpine avatar BrownAlpine commented on July 30, 2024

do you know why as I add rows with plot_grid the size of my plots shrinks to the point where they become unreadable? (let's say I want to print 6 plots ncol=6, nrow=1 and it works fine. Then I add 24 other plots to get to ncol=6, nrow=5 and it does not work anymore). I posted it here

I have also used the patchwork library but with the same results

from cowplot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.