Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add both margins to facet_grid but not the combination

I want to show both the right and the bottom margin, but not the combination of both. Is this possible with facet_grid?

library(ggplot2)

ggplot(mtcars, aes(x = hp, group = vs)) + 
  geom_density() + 
  facet_grid(am ~ vs, switch = "y", margins = TRUE) 

I tried to fix it by manually providing the factors to the margin argument but that doesn't help. Also, the R Documentation does not help me.

Current outcome

The current outcome

Desired outcome

The desired outcome

like image 422
Michiel uit het Broek Avatar asked Dec 23 '22 02:12

Michiel uit het Broek


1 Answers

such things are trial an error for me since I don't see the bigger pictures how the entries in gt relate to the end form:

library(ggplot2)

p<-
ggplot(mtcars, aes(x = hp, group = vs)) + 
    geom_density() + 
    facet_grid(am ~ vs, switch = "y", margins = TRUE)

gt = ggplotGrob(p)

keep <- !grepl("[3br]-3$",gt$layout$name)

gt$layout <- gt$layout[keep,]
gt$grobs  <- gt$grobs[keep]

plot(gt)

enter image description here

like image 129
Andre Elrico Avatar answered Jan 02 '23 16:01

Andre Elrico