Indy
Indy

Reputation: 21

Why do ggmagnify() zoomed-in insets are off-limit?

First, I am not very familiar with the R language and learn it. I use R4.31, RStudio , ggplot2 3.5.1, dplyr 1.1.4 and ggmagnify 0.4.1.

Second, most of my needs are plots so ggplot is very useful. This week-end, I played with the ggmagnify package and it gave weird displays.

With this code, I get a regular plot, as intended

install.packages("ggmagnify", repos = c("https://hughjonesd.r-universe.dev", 
                                        "https://cloud.r-project.org"))

library(ggplot2)
library(dplyr)
library(ggmagnify)

starwars %>% 
  
  ggplot(mapping = aes(x = mass,
                       y = height,
                       color = sex)) +
  
  geom_point(show.legend = FALSE) +
  
  geom_smooth(se = FALSE,
              method = "lm") +
  
  labs(x = "masse en kg",
       y = "taille en cm") +
  
  theme_bw() +
  
  geom_magnify(from = c(xmin = 0, xmax = 200, ymin = 50, ymax = 275),
               to = c(xmin = 500, xmax = 750, ymin = 50, ymax = 140))

plot as intented

So far, so good, but, when I try this code with a condition, the plot is not as intented:

starwars %>% 
  
  ggplot(mapping = aes(x = mass,
                       y = height,
                       color = sex)) +
  
  geom_point(show.legend = FALSE) +
  
  geom_smooth(se = FALSE,
              method = "lm") +
  
  labs(x = "masse en kg",
       y = "taille en cm") +
  
  theme_bw() +
  
  geom_magnify(mapping = aes(from = sex == "female" & mass < 100),
               to = c(xmin = 500, xmax = 750, ymin = 50, ymax = 140),  
               zoom.size = 2)

plot not as intented

Then, I tried to get a double zoom (zoom2 should be created from zoom1), to see what happens. The code :

starwars %>% 
  
  ggplot(mapping = aes(x = mass,
                       y = height,
                       color = sex)) +
  
  geom_point(show.legend = FALSE) +
  
  geom_smooth(se = FALSE,
              method = "lm") +
  
  labs(x = "mass in kg",
       y = "height in cm") +
  
  theme_bw() +
  
  geom_magnify(mapping = aes(from = sex == "female" & mass < 100),
               to = c(xmin = 500, xmax = 750, ymin = 50, ymax = 140),  
               zoom.size = 2) +
  
  geom_magnify(from = c(xmin = 550, xmax = 700, ymin = 100, ymax = 125),
               to = c(xmin = 1000, xmax = 1250, ymin = 200, ymax = 250))

plot not as intented 02

I do not understand 2 points:

  geom_magnify(mapping = aes(from = sex == "female" & mass < 100),
               to = c(xmin = 500, xmax = 750, ymin = 50, ymax = 140))

gives the same result as this one ?

geom_magnify(mapping = aes(from = mass < 100),
               to = c(xmin = 500, xmax = 750, ymin = 50, ymax = 140))

Fixes I tried include:

Any help would be appreciated to get a better understanding of where and how I got wrong.

Have a nice day.

Upvotes: 0

Views: 34

Answers (0)

Related Questions