In ggmosaic
, I want every cell stand for the value sumx
, but the geom_mosaic can't support it (Default, in geom_mosaic ,the cell stand the numbers of the data rows, like geom_histogram ).
Below code, i repeat the rows data according the value sumx
, it's working well .
Is the any way to simplify this or have replacement code/geom_xx ? When the value is huge, current way maybe inefficiency. Thank!
library(tidyverse)
library(ggmosaic)
df <- diamonds %>% group_by(cut,color) %>% summarise(sumx = sum(x))
df2 <- df[rep(c(1:nrow(df)),df$sumx),]
ggplot(data = df2) +
geom_mosaic(aes(x = product(cut),fill=color))
As geom_mosaic
supports a weight
aesthetic you can achieve your desired result based on the aggregated data frame by mapping sumx
on weight
:
library(tidyverse)
library(ggmosaic)
df <- diamonds |>
summarise(sumx = sum(x), .by = c(cut, color))
ggplot(data = df) +
geom_mosaic(aes(x = product(cut), fill = color, weight = sumx))