1

I want to create a mosicplot (2x2), where the counterpart have the same color.

So the mosaicplot should have a colorpalette like:

enter image description here

Here is the structure of the data i used for the graphic:

data <- data.frame(G1 = c("A", "B", "B", "A", "B", "B", "B", "B", "B", "B", "B"),
                   G2 = c("A", "A", "A", "B", "A", "A", "A","A", "B", "B", "B"))

I already tried to solve this with defining a third variable, which is then defining the colors:

data$C1 <- ifelse((data$G1 == "A" & data$G2 == "A") | (data$G1 == "B" & data$G2 == "B") ,
                    data$C1 <- "1", data$C1 <- "0")
ggplot(data = data) +
  geom_mosaic(aes(x = product(G1, G2), fill = C1))

Unfortunately this results in:

enter image description here

Maybe someone knows a solution, whereby these little stripes are not visible (for example the blue line in field G1 == B and C1:G2 = 1:A)?

Thanks

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
francis
  • 11
  • 2
  • What stripes are you referring to? If you mean the plot background, add `theme_void()` to your ggplot2 code. – Phil Apr 04 '23 at 14:16
  • 1
    Could you please share some reproducible code and data using `dput`? – Quinten Apr 04 '23 at 16:43
  • @Phil, for example, it would be great, when the blue line in the field G1 = B and C1:G2 = 1:A disappears. – francis Apr 05 '23 at 08:58

1 Answers1

0

I can reproduce this, & the reason is that ggmosaic's prodcalc function calculates the position for every combination of c(G1, G2, C1) in your example, even the ones with count = 0. These zero-width rectangles lead to 2 issues:

  1. They show up as super-thin lines on the mosaic plot, which you have already observed;
  2. They are also taken into account when offset is applied, so even if we hide the them, alignment for the remaining rectangles in the mosaic plot will go out of sync.

The second issue can be amplified with a higher value for offset (default is 0.01):

p <- ggplot(data = data) +
  geom_mosaic(aes(x = product(G1, G2), fill = C1),
              offset = 0.1)
p

plot with high offset

Adjusting the transparency of these zero-width rectangles will address issue 1 but not issue 2:

ggplot(data) +
  geom_mosaic(aes(x = product(G1, G2), fill = C1,
                  alpha = ifelse(after_stat(xmin) == after_stat(xmax), 0, 1)),
              offset = 0.1) +
  scale_alpha_identity()

plot with high offset + transparency tweak

I don't have a perfect solution for this, but if you are willing to tolerate lack of space between the rectangles, setting offset = 0 and applying a white outline over everything (to replace the offset's function in distinguishing each rectangle from the rest) can mitigate the appearance:

ggplot(data) +
  geom_mosaic(aes(x = product(G1, G2), fill = C1),
              offset = 0, colour = "white")

plot from workaround with zero offset

Z.Lin
  • 28,055
  • 6
  • 54
  • 94