lemon
lemon copied to clipboard
`facet_rep_grid` layout spacing issues with R 4.0.0
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:
and this is how it looks on R 4.0.0
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
.
I have the same problem
The issue here is that your y-axis labels are very wide, so the same "spacing" is carried over to the second facet.
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
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.
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 :-(
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
Hej.
I also very the very same problem. I hope this issue can still be solved -- Lemon is a great tool!
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!
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')
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.
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')