admin管理员组

文章数量:1431037

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(";, 
                                        ";))

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))

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)

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))

I do not understand 2 points:

  • why the small zoom windows are offset while the limits in the to= argument are within the axis limits?
  • why this code chunk
  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:

  • changing the value or commenting out of the zoom.size = argument
  • remove NAs with drop_na(mass, height)
  • changing the values of from = and to =

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

Have a nice day.

本文标签: rWhy do ggmagnify() zoomedin insets are offlimitStack Overflow