mint
mint

Reputation: 9

Is there an R function in ggplot to limit a plot to the maximum data range?

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!

Upvotes: 0

Views: 169

Answers (2)

MalditoBarbudo
MalditoBarbudo

Reputation: 2015

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.

Upvotes: 1

bikeactuary
bikeactuary

Reputation: 497

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)) )

Upvotes: 0

Related Questions