0

Odd question, but I can't seem to find a neat way to manage it! I'm making a number of scientific figures in R--each has a whole bunch of plots (via ggplot2) stacked on top of each other (with plot_grid), matched up along the same x axis. So far so good, and I'm almost ready to export and polish up all of the figures in illustrator.

The last step is to impose limits on each plot such that space between each plot in the stacks is as narrow as possible. I've done it in a first pass way with:

test1<-ggplot(mtcars, aes(mpg, hp)) + 
  geom_line() +
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
        axis.line.y.right = element_line(colour = "black", size=1), 
        axis.line.y.left = element_line(colour = "black", size=1),
        axis.ticks.y.right = element_blank(),
        axis.text.y.right = element_blank(),        
        axis.title.x = element_blank(),    
        axis.ticks.x = element_blank(),    
        axis.line.x.bottom = element_blank(),    
        axis.text.x =element_blank(),
        axis.line = element_line(colour = "black", size =1),)

test2<-ggplot(mtcars, aes(mpg, drat)) + 
  geom_line() +
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
        axis.line.y.right = element_line(colour = "black", size=1), 
        axis.line.y.left = element_line(colour = "black", size=1),
        axis.ticks.y.right = element_blank(),
        axis.text.y.right = element_blank(),        
        axis.title.x = element_blank(),    
        axis.ticks.x = element_blank(),    
        axis.line.x.bottom = element_blank(),    
        axis.text.x =element_blank(),
        axis.line = element_line(colour = "black", size =1),)

test3<-ggplot(mtcars, aes(mpg, wt)) + 
  geom_line() + 
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"),
                      axis.line.y.right = element_line(colour = "black", size=1), 
                      axis.line.y.left = element_line(colour = "black", size=1),
                      axis.ticks.y.right = element_blank(),
                      axis.text.y.right = element_blank(),        
                      axis.title.x = element_blank(),    
                      axis.ticks.x = element_blank(),    
                      axis.line.x.bottom = element_blank(),    
                      axis.text.x =element_blank(),
                      axis.line = element_line(colour = "black", size =1),)

test<- plot_grid(test1, test2, test3, ncol=1, align="v", rel_heights = c(1,1,1))
test

It looks like something I've done in the theme bit. Not sure--but help would be appreciated!

camille
  • 16,432
  • 18
  • 38
  • 60
mint
  • 9
  • 2
  • Can you make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? Without data, code, or output, we're left with just our imaginations to know what these charts are and what you're trying to do. And what's `grid_plot`? – camille Nov 24 '19 at 01:19
  • Hi Camille--thanks for your response. I can't actually release any data, code, or output, according to work rules, but I probably can explain better! I have 7 simple line plots made in ggplot, and plot_grid (oops!) is a tool that I used to stack them vertically into a single figure. The 7 are arranged in this "stack" as the x axes are consistent between them. Think about it as a double y plot, but not so impossible to read as most double y plots. I want to limit the space between each of the 7 plots by removing any kind of a buffer that ggplot automatically generates around a plot. – mint Nov 24 '19 at 01:29
  • Click the link I posted. You don't have to post your exact data—you can make up dummy data or use data that ships with a commonly available package. But right now there's very little we could use to help you, or that would make this helpful to future users – camille Nov 24 '19 at 01:37
  • edited! That should be enough to understand the issue? – mint Nov 24 '19 at 02:05

2 Answers2

1

If you want the plots stacked with no space between them, you need to play with the plot.margin value in the theme call. You can assign negative values, and different units. So, something like this could do what you want:

library(ggplot2)
library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************

my_theme <- theme(
  plot.margin = unit(c(0, 0, -0.3, 0), "lines"),
  axis.title.x = element_blank(),    
  # axis.line = element_line(colour = "black", size =1),
  axis.line.y.right = element_line(colour = "black", size=1), 
  axis.line.y.left = element_line(colour = "black", size=1),
  axis.line.x.bottom = element_blank(),
  # axis.ticks.y.right = element_blank(),
  # axis.ticks.x = element_blank(),    
  # axis.text.y.right = element_blank(),        
  axis.text.x =element_blank(),
)

test1<-ggplot(mtcars, aes(mpg, hp)) + 
  geom_line() +
  my_theme

test2<-ggplot(mtcars, aes(mpg, drat)) + 
  geom_line() +
  my_theme

test3<-ggplot(mtcars, aes(mpg, wt)) + 
  geom_line() + 
  my_theme

test<- plot_grid(test1, test2, test3, ncol=1, align="v", rel_heights = c(1,1,1))
test

Created on 2019-11-24 by the reprex package (v0.3.0)

Just play with the plot.margin bottom or top margins to get what you need. Also, I commented some lines of your theme call, as they were unnecesary, at least with the example provided. Feel free to uncomment if needed with your original data.

MalditoBarbudo
  • 1,815
  • 12
  • 18
0

Don’t really understand the question and you need to post a reproducible example.

Are you asking how to limit the axis ranges? If so

+ scale_x_continuous(limits = c(0, 1))

Will adjust the x axis range. Similar function for y. Just adjust the 0,1. Add it for each of your plots in the grid.

Update:

gglist <- lapply(gglist, function(x) x + scale_x_continuous(limits = c(0, 1)) )
bikeactuary
  • 447
  • 4
  • 18
  • Yep, that's getting closer to my question! I'm looking to understand why there's a gap around each of the plots that are stacked. I'd just like to see if there's a way around adding a y limit to each and every plot, as there are nearly 50 all in all! – mint Nov 24 '19 at 02:08
  • Ok - you can write a function that takes a ggplot and adds the scale_x_continuous. Then loop through all the ggplots in your grid to apply it – bikeactuary Nov 24 '19 at 02:14
  • Hmm, that doesn't seem to solve it. the scale_x_continuous bit doesn't remove the gaps added in the vertical direction--nor would scale_y_continuous, as I would still have to know the limits of the particular range of data that I am plotting, and then input them for each separate piece. – mint Nov 24 '19 at 02:53
  • You will need to create a reproducible example as others have said – bikeactuary Nov 24 '19 at 03:19
  • yup--it's up there already! If we were looking at the example I've included, I'd want to reduce the space between the hp and drat plots such that the plotted lines nearly touch – mint Nov 24 '19 at 03:27