Mosaicplot with ggplot2 too small

121 views Asked by At

I'm trying to visualize a mosaic plot with ggplot2 and ggmosaic. I'm having the problem that the mosaicplot itself is really small and the legend too big. Do you have an idea of how can I arrange this?

The code I'm using is the following:

Create a mosaic plot


mosaic_plot_color <- ggplot(data = longdata) +
  geom_mosaic(aes(x = product(Category, variable), fill=Category ,weight = `value`), offset=0.010) +
  scale_fill_brewer(palette = 2)+
  #scale_color_brewer(palette=3)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = .5), 
        legend.key.height = unit(.1, 'cm'), 
        legend.key.width = unit(.4, 'cm'))+
  labs(x ="Frequency of social contact" , y = "Type of street (microm)", weight = "Number of observations", title = "Social contact with locals living in different house typologies")

Mosaic Plot Output

Reproducible data: The dput output from my data is the following:

structure(list(Category = c("1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), levels = c("Never", "Rarely", 
"Every month", "Every Week", "More than once a week", "Daily"
), class = "factor"), value = c(0, 24, 11, 35, 11, 14, 0, 4, 
26, 34, 28, 20, 20, 8, 6, 24, 22, 39, 14, 13, 3, 14, 40, 59, 
54, 48, 14, 6, 1, 26, 46, 36, 37, 17, 4, 4, 42, 52, 63, 40, 13, 
0)), row.names = c(NA, -42L), class = "data.frame")

I tried scaling the Legend with the following part of the code expecting that the mosaic plot would change the size, but did not work as I expected.

  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = .5), 
        **legend.key.height = unit(.1, 'cm'), 
        legend.key.width = unit(.4, 'cm'))**+

I would like to reach something like this, just with colors like I have in the first image: MosaicPlotrightscale

I would appreciate any hints from your side. Many thanks in advance, Victoria


To whom might be interested: I could reach my objective and this is how the code looks like:

ggplot(data=longdata)+
  geom_mosaic(aes(x=product(variable,Category), fill=Category,weight = `value`))+
  theme(axis.text.x = element_text(size=4, angle = 45, hjust = 1))+
  theme(axis.text.y = element_text(size=5, angle = 45, hjust = 1))+
  scale_fill_brewer(palette=2)

The plot looks like this: Finalplot

1

There are 1 answers

0
Andy Baxter On

Another solution would be to str_wrap your long variable names:

library(tidyverse)
library(ggmosaic)

longdata |>
  mutate(Category = str_wrap(Category, 30)) |>
  ggplot() +
  geom_mosaic(aes(
    x = product(Category, variable),
    fill = Category ,
    weight = `value`
  ), offset = 0.010) +
  scale_fill_brewer(palette = 2) +
  theme(
    axis.text.x = element_text(
      angle = 45,
      hjust = 1,
      vjust = 1
    ),
    legend.key.height = unit(0.8, 'cm'),
    legend.key.width = unit(.4, 'cm')
  ) +
  labs(
    x = "Frequency of social contact" ,
    y = "Type of street (microm)",
    weight = "Number of observations",
    title = "Social contact with locals living in different house typologies"
  )