Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow inset_element() to handle facet merging #344

Closed
matiasandina opened this issue Dec 21, 2023 · 1 comment
Closed

Comments

@matiasandina
Copy link

I am trying to plot a facet plot. My idea is to display inset plots according to the facet. Here's a reproducible example using mtcars. The idea would be to get density plots for am=0 on the proper (am=0) facet at (0.5, 0.5, 1, 1). Same idea for am=1.

library(tidyverse)
library(patchwork)
p1 <- ggplot(mtcars, aes(cyl, mpg)) + 
  geom_point(color = "red") +
  facet_wrap(~am)
p2 <- ggplot(mtcars, aes(hp)) + 
  geom_density(aes(fill=factor(cyl), color=factor(cyl))) +
  facet_wrap(~am)
p1

p2

I understand why this is not functioning the way I want it. I am not sure if this should be an acceptable feature request to the library. Ideally, patchwork would automagically realize that the inset has to go on proper panels.

p1 + inset_element(p2, 0.5, 0.5, 1, 1)

Created on 2023-12-11 with reprex v2.0.2

The expected output would be:

image

I came up with a solution to individually subset data and return the inset plot to be later used in a layout. I have also asked this question and there is a proposed answer that is a bit more elegant than my method here. I feel that this use case is a somewhat complex functionality that people end up having to customize like here. It would be great if patchwork could handle this internally.

@thomasp85
Copy link
Owner

I understand the wish for something like this but it will not happen for multitudes of technical reasons :-)

The way forward is to subset the data manually as you've already arrived at

@thomasp85 thomasp85 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants