I'm creating plots using ggplot2 and ggmagnify in R. I have noticed that when I set the ggmagnify 'from' argument too close to the minimum plot axis values, that magnified plot no longer shows up. I'm not sure if the issue is specific to using scale_x_continuous
/ scale_y_continous
or if it occurs anytime the magnified box gets too close to the plot edges. It does seem to be an issue with both axes. I have also noticed that on my actual plots, the minimum value I can enter before the plot disappears is smaller for plots with smaller axis value ranges (e.g. a plot with an axis from 0 to 20 works with a from value down to 0.05 while a plot with an axis from 0 to 26 only works with a value down to 0.22).
Is this a bug/issue with ggmagnify or am I missing an element in ggplot or ggmagnify that avoids this issue?
R: 4.3.1; ggplot2: 3.4.3; ggmagnify: 0.2.0.9
library(ggplot2)
library(ggmagnify)
library(cowplot)
set.seed(1)
x <- seq(0,10,1)
y <- sample(x,11)
plist <- vector(mode = "list", length = 4)
# Magnified plot shows up with from = c(0.25, 5, 0.25, 5)
plist[[1]] <- ggplot(data=data.frame(x,y)) +
geom_jitter(aes(x=x,y=y), height = 0) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 10)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 10)) +
coord_cartesian(clip="off") +
geom_magnify(from=c(0.25, 5, 0.25, 5),to=c(11, 21, 0.45, 9.5), axes = "xy")
# Magnified plot does not show up with from = c(0.20, 5, 0.20, 5)
plist[[2]] <- ggplot(data=data.frame(x,y)) +
geom_jitter(aes(x=x,y=y), height = 0) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 10)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 10)) +
coord_cartesian(clip="off") +
geom_magnify(from=c(0.20, 5, 0.20, 5),to=c(11, 21, 0.45, 9.5), axes = "xy")
plot_grid(plotlist = plist, align = "hv", byrow = FALSE)
One option to fix your issue would be to set the limits via coord_cartesian
instead of via the scale. The latter will remove all data points and all "ink" which falls outside of the limits, whereas setting the limits via the coord does not:
library(ggplot2)
library(ggmagnify)
#> Warning: package 'ggmagnify' was built under R version 4.3.1
library(cowplot)
set.seed(1)
x <- seq(0, 10, 1)
y <- sample(x, 11)
plist <- vector(mode = "list", length = 4)
# Magnified plot shows up with from = c(0.25, 5, 0.25, 5)
plist[[1]] <- ggplot(data = data.frame(x, y)) +
geom_jitter(aes(x = x, y = y), height = 0) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
coord_cartesian(xlim = c(0, 10), ylim = c(0, 10), clip = "off") +
geom_magnify(from = c(0.25, 5, 0.25, 5), to = c(11, 21, 0.45, 9.5), axes = "xy")
# Magnified plot does not show up with from = c(0.20, 5, 0.20, 5)
plist[[2]] <- ggplot(data = data.frame(x, y)) +
geom_jitter(aes(x = x, y = y), height = 0) +
scale_x_continuous(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
coord_cartesian(xlim = c(0, 10), ylim = c(0, 10), clip = "off") +
geom_magnify(from = c(0.20, 5, 0.20, 5), to = c(11, 21, 0.45, 9.5), axes = "xy")
plot_grid(plotlist = plist, align = "hv", byrow = FALSE)