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

insert_xaxis_grob with facet_grid, more than one marginal plot on x-axis #151

Open
ccshao opened this issue Dec 3, 2019 · 1 comment
Open

Comments

@ccshao
Copy link

ccshao commented Dec 3, 2019

I would like to add the marginal plots to a gglot2 object, so the axis match. However, I could not manage it with the insert_xaxis_grob and axis_canvas.

A very simple example

The major plot:

p01 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  theme_cowplot()

The margin, with facet_grid

p05 <- axis_canvas(p01, axis = "x") +
  geom_histogram(data = iris, aes(Sepal.Length), alpha = 0.8) +
  scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set2")) +
  scale_fill_manual(values = RColorBrewer::brewer.pal(4, "Set2")) +
  facet_grid(vars(Species))

Screenshot from 2019-12-03 09-39-11

combined_plot <- insert_xaxis_grob(p01, p05, position = "bottom")
it gives the warning:

stat_bin()usingbins = 30. Pick better value with binwidth. Warning messages: 1: Removed 6 rows containing missing values (geom_bar). 2: In get_panel(grob) : Multiple panels found; returning the first one. Select a panel with panelor return all withreturn_all = TRUE`.

and indeed only the first one is used.

Screenshot from 2019-12-03 09-38-51

Any suggestions to add more than one marginal plots on the axis, as I would like to compare the distribution between different groups.

Thanks very much.

@zhuoxinChen-code
Copy link

try this:
library(patchwork)
combined_plot <- insert_xaxis_grob(p01, wrap_plots(p05), position = "bottom") #wrap_plots would combine them as one
ggdraw(combined_plot)

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