0

Is there a way to make the chart area sizes equal in two plots? When using these two charts in a presentation, it would be nice if the rectangles just would split and "stay in place".

I was not able to do this by using plot.margin when creating a presentation in slidy.

unsynchronized mosaic plots

markus
  • 25,843
  • 5
  • 39
  • 58
Erich Neuwirth
  • 943
  • 7
  • 13
  • 1
    could you please share your code? It would be a lot easier to recreate your problem when you add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) In general the respond rate is much higher when people don't have to create there own examples first – mischva11 Nov 12 '18 at 12:01

2 Answers2

0

Here is some code demonstrating the problem.
I would like the bottom borders of the grey chart area to be aligned.

## ------------------------------------------------------------------------
library(tidyverse)
library(ggmosaic)
library(gridExtra)

## ------------------------------------------------------------------------
pruef <- tibble::tribble(
  ~Geschlecht, ~Studium, ~Test,   ~n,
          "m",   "MINT", "pos", 165L,
          "w",   "MINT", "pos",  60L,
          "m",    "HUM", "pos",  30L,
          "w",    "HUM", "pos", 105L,
          "m",   "MINT", "neg", 135L,
          "w",   "MINT", "neg",  40L,
          "m",    "HUM", "neg",  70L,
          "w",    "HUM", "neg", 195L
  )

## ------------------------------------------------------------------------
pruef %>%
  ggplot() +
  geom_mosaic(aes(
    x = product(Test),
    weight = n,
    fill = Test
  ), divider = "vspine") +
  guides(fill = guide_legend(reverse = TRUE)) +
  labs(x = "", y = "") +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) -> p1


## ------------------------------------------------------------------------
pruef %>%
  ggplot() +
  geom_mosaic(aes(x=product(Studium,Geschlecht),weight=n,fill=Test),divider=ddecker()) +
  guides(fill=guide_legend(reverse=TRUE)) +
  labs(x="",y="") +
  theme(axis.ticks.x = element_blank(),
        axis.text.x=element_text(angle=90)) ->p2

## ------------------------------------------------------------------------
grid.arrange(p1,p2,nrow=1)

enter image description here

Erich Neuwirth
  • 943
  • 7
  • 13
0

Use :

library(cowplot)
plot_grid(p1, p2, align = "h")

1

If you want the duplicate legend removed, you will need to nest your plot_grid() in another plot_grid()(a bit hacky if your plots or code change a lot).


Alternatively, trade stability for comfort and try :

library(egg)
ggarrange(p1 + theme(legend.position = "none"), p2, nrow = 1)

2

Roman
  • 4,744
  • 2
  • 16
  • 58