I want to create a mosicplot (2x2), where the counterpart have the same color.
So the mosaicplot should have a colorpalette like:
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:
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


I can reproduce this, & the reason is that ggmosaic's
prodcalcfunction calculates the position for every combination ofc(G1, G2, C1)in your example, even the ones with count = 0. These zero-width rectangles lead to 2 issues:The second issue can be amplified with a higher value for offset (default is 0.01):
Adjusting the transparency of these zero-width rectangles will address issue 1 but not issue 2:
I don't have a perfect solution for this, but if you are willing to tolerate lack of space between the rectangles, setting
offset = 0and applying a white outline over everything (to replace the offset's function in distinguishing each rectangle from the rest) can mitigate the appearance: