Define the fill of the zoomed panel using facet_zoom in ggforce

217 Views Asked by At

I want to fill the zoomed panel to match the default grey of facet_zoom in ggforce.

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
   geom_point() +
   facet_zoom(x = Species == 'versicolor') +
   theme_bw()

enter image description hereI want the zoomed panel also to have the grey background. Any thoughts?

1

There are 1 best solutions below

0
jsirgo On

I didn't want to modify facet_zoom and I was hoping someone might know of an elegant way to modify the color of the zoom panels like with zoom.x = element_rect(fill = "grey", color = NA), zoom.y = element_rect(fill = "grey", color = NA) within theme, but those only address the zoomed section of the un-zoomed panel and the callout to the zoomed panel. I do have a more brute force way to do it using annotate:

ggplot(iris, aes(Petal.Length, Petal.Width, colour = Species)) +
    annotate("rect", xmin = data.table(iris)[Species == 'versicolor', min(Petal.Length)] * 0.965, xmax = data.table(iris)[Species == 'versicolor', max(Petal.Length)] * 1.02, ymin = -Inf, ymax = Inf, fill="light grey", alpha=0.3) +
    geom_point() +
    facet_zoom(x = Species == 'versicolor') +
    theme_bw() 

The challenge to to find the boundaries of the x and/or y axis when they're not explicit in facet_zoom. I had to hunt and peck to get them to align.

enter image description here