Home > Back-end >  ggforce adds horizontal zoom background when "horizontal = FALSE"
ggforce adds horizontal zoom background when "horizontal = FALSE"

Time:10-21

Consider this relatively simple plot (the red color was added only to demonstrate the problem a bit better).

library(ggplot2)
library(ggforce)

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species))  
    geom_point()  
    facet_zoom(x = Species == "versicolor")  
    theme(zoom = element_rect(fill = "red"))

enter image description here

Now I want to adjust the ylim of the zoomed part (the lower plot).

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species))  
    geom_point()  
    facet_zoom(
        x = Species == "versicolor",
        ylim = c(0.5, 2.5),
        horizontal = FALSE
    )  
    theme(zoom = element_rect(fill = "red"))

enter image description here

So we add ylim and horizontal = FALSE cause we only want vertical zoom. However, ggforce adds a horizontal zoom indicator nonetheless (the red horizontal bar). Is it possible to get rid of that? So that we only get the vertical zoom indicator like above?

Thank you so much.

CodePudding user response:

IMHO you misunderstood the working of horizontal. As I read the docs, horizontal is used to the position the zoom panel (see ?facet_zoom):

How should the zoom panel be positioned relative to the full data panel?

Moreover using x = Species == "versicolor" you specify that you want to zoom the range of x to the versicolor data. And when adding ylim you additionally add a zoom of the y axis. And that's what the "zoom indicators" are showing.

But a fix would be to set the fill color for the unwanted "zoom indicator" to NA via theme options:

library(ggplot2)
library(ggforce)

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species))  
  geom_point()  
  facet_zoom(
    x = Species == "versicolor",
    ylim = c(0.5, 2.5),
    horizontal = FALSE
  )  
  theme(zoom.x = element_rect(fill = "red"), zoom.y = element_rect(fill = NA))

  • Related