hughjonesd / ggmagnify

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

Magnified plot not filling the whole plot area #26

Closed paulobarros closed 7 months ago

paulobarros commented 7 months ago

First of all, amazing package! Thank you so much!

Describe the bug I cannot make the magnified plot to fill the whole area of the inset. I am using the option with a provided inset plot, but it seems that it is being constrained somehow and not filling the whole inset area declare in the to argument. When I tried with annotate_custom it works fine, but with geom_magnify I could not make it work. I've set limits, breaks, and still nothing.

image

To Reproduce

library(tidyverse)
library(ggmagnify)

pca <- read.table("https://raw.githubusercontent.com/paulobarros/assets/main/pcs.txt", col.names = c("ind","PC1","PC2","PC3","gg")) |>
    select(-ind) |>
    mutate(gg = factor(gg)) |>
    mutate(colors = case_when(
        gg == "BEL" ~ alpha("#124559",.75),
        gg == "BRO" ~ alpha("#e0aaff",.75),
        gg == "CC" ~ alpha("#00b4d8",.75),
        gg == "China" ~ alpha("#80ed99",.75),
        gg == "WEL" ~ alpha("#bb9457",.75),
        gg == "TT" ~ alpha("#ff006e",.75),
        gg == "Nigeria" ~ alpha("#fca311",.75),
        gg == "Indio" ~ alpha("#6c757d",.75),
        gg == "GG" ~ alpha("#f07167",.75)
    ),
    lines = case_when(
        gg == "BEL" ~ alpha("#124559",.95),
        gg == "BRO" ~ alpha("#e0aaff",.95),
        gg == "CC" ~ alpha("#00b4d8",.95),
        gg == "China" ~ alpha("#80ed99",.95),
        gg == "WEL" ~ alpha("#bb9457",.95),
        gg == "TT" ~ alpha("#ff006e",.95),
        gg == "Nigeria" ~ alpha("#fca311",.95),
        gg == "Indio" ~ alpha("#6c757d",.95),
        gg == "GG" ~ alpha("#f07167",.95)
    ))

ggColors <- pca$colors |> unique()
ggLines <- pca$lines |> unique()

from <- c(xmin = -0.015,
          xmax = 0.015,
          ymin = -0.065,
          ymax = -0.02)

to <- c(0.05, 0.2, -0.04,0.03)

inset <- pca |> 
filter(!gg %in% c("TT","China")) |>
ggplot(aes(x = PC1, y = PC2,)) +
geom_point(aes(fill = gg),
color = alpha("black",.75),
shape = 21,
size = 6,
show.legend = FALSE) +
scale_fill_manual(values = ggColors |> unique()) +
labs(x = element_blank(), y  = element_blank()) +
#scale_color_manual(values = ggLines |> unique()) +
theme_bw(base_family = "Barlow Condensed", base_size = 16) +
scale_x_continuous(breaks = (seq(-0.005, 0.001, 0.002)), limits = c(-0.005,0.0011)) +
scale_y_continuous(breaks = (seq(-0.06, -0.023, 0.015)), limits = c(-0.06,-0.023))

pca |> 
#filter(!gg %in% c("TT","China")) |>
ggplot(aes(x = PC1, y = PC2,)) +
geom_point(aes(fill = gg),
color = alpha("black",.75),
shape = 21,
size = 6) +
scale_fill_manual(values = ggColors |> unique()) +
labs(x = "PC1 (10.34%)",
     y = "PC2 (7.73%)",) +
#scale_color_manual(values = ggLines |> unique()) +
theme_bw(base_family = "Barlow Condensed", base_size = 16) +
geom_magnify(from = from,
            to = to,
            shadow = TRUE,
            plot = inset,
            axes = "xy"
            )

Expected behavior I would like the result to be like this version I was able to make with annotate_custom

image

Other information

Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Pop!_OS 22.04 LTS

other attached packages:
 [1] ggmagnify_0.4.0.9000 here_1.0.1          
 [3] plotly_4.10.4        lubridate_1.9.3     
 [5] forcats_1.0.0        stringr_1.5.1       
 [7] dplyr_1.1.4          purrr_1.0.2         
 [9] readr_2.1.5          tidyr_1.3.1         
[11] tibble_3.2.1         ggplot2_3.5.0       
[13] tidyverse_2.0.0     
hughjonesd commented 7 months ago

Thanks for the bug report. Please could you provide a reproducible example, i.e. one that I can actually run. (Hint: I don't have a data set called PCA.)

paulobarros commented 7 months ago

Sorry! My bad! Updated the previous comment with the full code.

hughjonesd commented 7 months ago

And what are from and to? Maybe just double check it's reproducible by copy-pasting into a new R session

paulobarros commented 7 months ago

Sorry again, copy-pasted into a new R session and it runs.

hughjonesd commented 7 months ago

So I get: image

As I understand it, you want the inset plot to show only the expanded area, not the white space on either side.

Can I ask why something like the following code doesn't satisfy your needs? It doesn't use the plot argument and simply expands the area that you are interested in:

from <- c(
          xmin = -0.005,
          xmax = 0.005,
          ymin = -0.065,
          ymax = -0.02)

to <- c(0.05, 0.2, -0.04,0.03)

pca |> 
  ggplot(aes(x = PC1, y = PC2)) +
  geom_point(aes(fill = gg), color = alpha("black",.75), shape = 21, size = 6) +
  scale_fill_manual(values = ggColors |> unique()) +
  labs(x = "PC1 (10.34%)", y = "PC2 (7.73%)",) +
  theme_bw(base_family = "Barlow Condensed", base_size = 16) +
  geom_magnify(from = from,
            to = to,
            shadow = TRUE,
            axes = "xy")

This gives me: image

hughjonesd commented 7 months ago

In any case, I think the reason for your bug is that the geom_magnify code overrides the limits of the inset plot - that's the way it works. So if you don't want it to do that, then indeed just using an annotation may be more flexible.

hughjonesd commented 7 months ago

You might also want to mess around with the scale.inset argument. E.g.

from <- c(xmin = -0.01,
          xmax = 0.005,
          ymin = -0.065,
          ymax = -0.02)

to <- c(0.05, 0.2, -0.04,0.03)

pca |> 
#filter(!gg %in% c("TT","China")) |>
ggplot(aes(x = PC1, y = PC2,)) +
geom_point(aes(fill = gg),
color = alpha("black",.75),
shape = 21,
size = 6) +
scale_fill_manual(values = ggColors |> unique()) +
labs(x = "PC1 (10.34%)",
     y = "PC2 (7.73%)",) +
#scale_color_manual(values = ggLines |> unique()) +
theme_bw(base_family = "Barlow Condensed", base_size = 16) +
geom_magnify(from = from,
            to = to,
            shadow = TRUE,
            axes = "xy",
            scale.inset = c(0.45, 1)
            )

which gives image

paulobarros commented 7 months ago

RTFM right? So sorry to take up your time! 🤦🏽‍♂️ And thank you again for the help and this awesome package!