lemon icon indicating copy to clipboard operation
lemon copied to clipboard

`facet_rep_grid` layout spacing issues with R 4.0.0

Open sam81 opened this issue 4 years ago • 7 comments

when saving a plot with facet_rep_grid the spacing between the panels is different in R 3.6.3 and R 4.0.0. This is an example (code to reproduce below) of how it looks like on R 3.6.3:

R version 3 6 3 (2020-02-29)

and this is how it looks on R 4.0.0

R version 4 0 0 (2020-04-24)

Code:

library(ggplot2)
library(lemon)

set.seed(123)
est=rnorm(6, sd=2)
dd = data.frame(lev=rep(c("Level 1", "Level 2", "Level 3"), 2),
                pos = rep(c("Pos 1", "Pos 2"), each=3),
                est,
                ciLow = est-abs(rnorm(6, 1, sd=3)),
                ciHigh = est+abs(rnorm(6, 1, sd=3))
                )

p = ggplot(dd, aes(x=lev, y=est))
p = p + geom_point(shape=1)
p = p + facet_rep_grid(~pos)
p = p + geom_errorbar(aes(ymin=ciLow, ymax=ciHigh), width=0)
p = p + coord_flip() + theme_bw()
ggsave(paste0(R.version.string, ".pdf"), plot=p, width=3.4, height=2, device=cairo_pdf)
ggsave(paste0(R.version.string, ".png"), plot=p, width=3.4, height=2) 

Here's the output of sessionInfo on R 3.6.3: session_infoR version 3.6.3 (2020-02-29).txt

and here's the output of sessionInfo on R 4.0.0: session_infoR version 4.0.0 (2020-04-24).txt

I suppose the issue may be related to the new version of grid under R 4.0.0, all packages except grid and tools are at the same version in my sessionInfo.

sam81 avatar May 02 '20 11:05 sam81

I have the same problem

raubreywhite avatar Jun 09 '20 12:06 raubreywhite

The issue here is that your y-axis labels are very wide, so the same "spacing" is carried over to the second facet.

raubreywhite avatar Jun 09 '20 12:06 raubreywhite

library(ggplot2)
library(lemon)

set.seed(123)
est=rnorm(6, sd=2)
dd = data.frame(lev=rep(c("Level 1", "Level 2", "Level 3"), 2),
                pos = rep(c("Posxxxxxxxxxxx 1", "Pos 2"), each=3),
                est,
                ciLow = est-abs(rnorm(6, 1, sd=3)),
                ciHigh = est+abs(rnorm(6, 1, sd=3))
)

p = ggplot(dd, aes(x=est, y=pos))
p = p + geom_point(shape=1)
p = p + facet_rep_wrap(~lev,ncol=1)
p = p + geom_errorbar(aes(ymin=ciLow, ymax=ciHigh), width=0)
p = p + coord_flip() + theme_bw()
p = p + theme(axis.text.x = element_text(angle = 90, hjust = 1, 
                                         vjust = 0.5))
p

image

raubreywhite avatar Jun 09 '20 14:06 raubreywhite

Great input, and good observation on the spacing being repeated. There were significant changes to ggplot2 with v.3.3.0/R v.4.0, which probably caused this.

I cannot make any promises on when to fix this, so pull requests are welcome.

stefanedwards avatar Jun 09 '20 14:06 stefanedwards

No word on when to fix this? It's been more than a year. The facet_rep_wrap/grid functions in lemon are really cool and now they are useless :-(

santiagosnchez avatar Aug 10 '21 12:08 santiagosnchez

Here is a kind of ugly workaround. It uses a replicated grob/gtable object of the y axis in the first panel without text, and copies it to the main grob. Then turns it back to a ggplot object.

library(ggplot2)
library(cowplot)
library(ggplotify)

x = seq(1, 10, length.out=100)
df = data.frame(
    x=rep(x, times=3), 
    y=c(x + rnorm(100, 1), x + rnorm(100, 2), x + rnorm(100, 3)), 
    z=rep(LETTERS[1:3], each=100))

p = ggplot(df, aes(x, y, color=z)) + geom_point() + facet_wrap(~z)

grob_main = as_grob(p)
grob_tmp = as_grob(p + theme(axis.text.y=element_blank()))

all_left_axis = grep("axis-l-", grob_main$layout$name)
left_axis_first_panel = grep("axis-l-1-1", grob_main$layout$name)
left_axis_remainder_panels = all_left_axis[ ! all_left_axis %in% left_axis_first_panel ]
for (i in left_axis_remainder_panels)
    grob_main$grobs[[i]] = grob_tmp$grobs[[left_axis_first_panel]]

p = as.ggplot(grob_main)
p

santiagosnchez avatar Aug 10 '21 16:08 santiagosnchez

Hej.

I also very the very same problem. I hope this issue can still be solved -- Lemon is a great tool!

g-pacheco avatar Jan 23 '22 19:01 g-pacheco

Hi, Sorry if I am being daft here, I am not too familiar with github. Should this issue be fixed in version 0.4.6 without me having to do anything additional? I am trying to use facet_rep_wrap() and am still encountering this issue. Any help would be very much appreciated!

tomscott024 avatar Apr 11 '23 12:04 tomscott024

lemon v. 0.4.6 did not include that fix. You'll have to install the version directly from github:

devtools::install_github("stefanedwards/lemon", ref='facet_spacing')

stefanedwards avatar Apr 12 '23 06:04 stefanedwards

Hello @stefanedwards,

Thanks a lot for fixing this in the facet_spacing version! Do you know when this modification will be incorporated into the main version?

Kind regards, George.

g-pacheco avatar Dec 07 '23 12:12 g-pacheco

Hi,

I'm very sorry, but I released an updated version of the package last month, where I had overlooked this issue. We'll have to wait another month or two before I can release a new version on CRAN (due to their policy of not too often updating packages).

In the meantime, I have merged the fix into the main version and it can be installed from github with

devtools::install_github("stefanedwards/lemon", ref='master')

stefanedwards avatar Dec 08 '23 09:12 stefanedwards