hughjonesd / ggmagnify

Create a magnified inset of part of a ggplot object
https://hughjonesd.github.io/ggmagnify/
Other
272 stars 3 forks source link

No plot shown if data is in layers #15

Open hughjonesd opened 1 year ago

hughjonesd commented 1 year ago

Example:

dfr <- data.frame(x = runif(100), y = runif(100))

# works
ggplot(dfr, aes(x, y)) + 
  geom_point() + 
  geom_magnify(from = c(.25, .35, .25, .35), to = c(0.2, .6, 0.5, .8))

# no magnify
ggplot() + 
  geom_point(data = dfr, mapping = aes(x,y)) +
  geom_magnify(from = c(.25, .35, .25, .35), to = c(0.2, .6, 0.5, .8))

There's no warning either.

Huangmp1996 commented 1 year ago

I seem to meet the same problem when trying to enlarge an area of a map using sf package:

# no magnify and warning either
ocean_map <- rnaturalearth::ne_coastline(scale = 'large', returnclass = c("sf"))
p <- ggplot() +
    geom_sf(data = ocean_map, aes(geometry = geometry)) + 
    theme_bw() + 
    geom_magnify(from = c(-20, 30, 37, 60), to = c(40, 120, 27, 70), shape = "outline")
p
hughjonesd commented 1 year ago

Fixing this will add a lot of complexity, I think. A workaround is simply to add your data to the geom_magnify() call:

ggplot() +
    geom_point(data = blah, ...) + 
    geom_magnify(data = blah, from = ..., to = ...)
Huangmp1996 commented 1 year ago

It solved my problem perfectly. Thank you!