Home > OS >  r: make expand_limit not add margin
r: make expand_limit not add margin

Time:12-08

I'm trying to make a plot that has no margins whatsoever, but I think using "expand_limits" is causing a margin to be added to my plot.

In the below example, I'm overlapping two plots to try and illustrate my point. I create two overlayed bar graphs and for one I use expand_limits to set the plot area to include y=1.5 as the maximum value. When I draw a rectangle with that same maximum value though, it becomes clear that there is no white space above that y max.

How can I stop the white space above y=1.5 (i.e., above the rectangle in the below) from appearing?

library(ggplot2)                        
library(tidyverse)
library(dplyr)
library(patchwork)

set.seed(16)
dataf1 <- tibble(
    percent=runif(4,0,1),
    group=c("Group A", "Group B", "Group C", "Group D")
)
dataf2 <- tibble(
    percent=runif(4,0,1),
    group=c("Group E", "Group F", "Group G", "Group H")
)

theme_update(axis.title = element_blank(),
      axis.text = element_blank(),
      axis.ticks =element_blank(),
      plot.margin=unit(c(0,0,0,0), "cm"),
      panel.spacing=unit(c(0,0,0,0), "cm"))

fig1 <- ggplot(data=dataf1, aes(y=percent,x=group))  
    geom_bar(stat="identity") 
 
fig2 <- ggplot(data=dataf2, aes(y=percent,x=group))  
    geom_bar(stat="identity")   theme(panel.background = element_blank())  
    expand_limits(y=1.5) 
    geom_rect(xmin=1,xmax=4,ymin=0,ymax=1.5,fill=NA,color="black")

layout <- c(
    area(t=1,l=1,b=8,r=8),
    area(t=3,l=2,b=10,r=9))

fig1 fig2 plot_layout(design=layout)

CodePudding user response:

This is just a reprex for my comment about the expand argument in scale_y_continuous().

library(ggplot2)                        
#> Warning: package 'ggplot2' was built under R version 4.1.1
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.1
#> Warning: package 'tidyr' was built under R version 4.1.1
#> Warning: package 'readr' was built under R version 4.1.1
library(dplyr)
library(patchwork)

set.seed(16)
dataf1 <- tibble(
  percent=runif(4,0,1),
  group=c("Group A", "Group B", "Group C", "Group D")
)
dataf2 <- tibble(
  percent=runif(4,0,1),
  group=c("Group E", "Group F", "Group G", "Group H")
)

theme_update(axis.title = element_blank(),
             axis.text = element_blank(),
             axis.ticks =element_blank(),
             plot.margin=unit(c(0,0,0,0), "cm"),
             panel.spacing=unit(c(0,0,0,0), "cm"))

fig1 <- ggplot(data=dataf1, aes(y=percent,x=group))  
  geom_bar(stat="identity") 

fig2 <- ggplot(data=dataf2, aes(y=percent,x=group))  
  geom_bar(stat="identity")   theme(panel.background = element_blank())  
  expand_limits(y=1.5) 
  geom_rect(xmin=1,xmax=4,ymin=0,ymax=1.5,fill=NA,color="black")

layout <- c(
  area(t=1,l=1,b=8,r=8),
  area(t=3,l=2,b=10,r=9))

fig1 fig2 plot_layout(design=layout)

fig3 <- fig2   scale_y_continuous(expand = c(0, 0))

fig1   fig3   plot_layout(design = layout)

Created on 2021-12-07 by the reprex package (v2.0.1)

  • Related