hughjonesd / ggmagnify

Create a magnified inset of part of a ggplot object

Home Page:https://hughjonesd.github.io/ggmagnify/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fill area between lines of magnified inset

etiennebacher opened this issue · comments

Hi, this is a very nice package! It would be cool if it was possible to fill the area between the lines so that we can get the red area like below:

image

Here's the code I have so far (coming from this SO answer):

library(ggplot2)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(rnaturalearth)
library(ggmagnify)

europe <- ne_countries(continent = "Europe", scale = 'medium',
                       type = 'map_units', returnclass = 'sf') 

your_map <- ggplot(europe) +
  geom_sf() +
  xlim(c(-20, 50)) +
  ylim(c(35, 70)) +
  theme_linedraw()

ggm <- your_map + 
  geom_magnify(
    from = c(9, 15, 42, 45),
    to = c(22, 36, 46, 52),
    colour = "red",
    linewidth = 1
  ) 

ggm


Add a 👍 on this post to show support for this feature.

I like it but I think it will be complex. In particular, it's easy to draw the magnified inset over the shaded area. But the target area isn't redrawn. So you have to draw an irregular shaded shape, between the two projection lines and not including the target area. (Which can be elliptical or even an arbitrary shape...!)

Sorry, but I think this is best done with a manual tool. I'll leave this request open though, and if enough people vote for it I will reconsider.

OK, this got some votes. It's now enabled in the experiment-shaded-projection branch, if you want to try it out. Just add e.g. proj.fill = alpha("grey", 0.2) to a geom_magnify call.

Warning: at present, using a ragg device on my machine (including the ragg RStudio backend) this causes a segfault. See r-lib/ragg#156. It seems to work OK with quartz or cairo backends. I'm actively investigating this.

This should now be in master and should no longer segfault.

That's awesome, thanks!