Closed andrewheiss closed 2 weeks ago
Using scale_y_product
solves this most of the way, but it requires that breaks and labels be manually defined:
ggplot(data=happy) +
geom_mosaic(aes(x=product(sex, marital), fill=sex)) +
scale_y_product(breaks=c(0.25, 0.75), labels=c("Male", "Female"))
Here I arbitrarily chose 0.25 and 0.75. Is there a way to automatically determine those breaks and labels?
@andrewheiss: Came here because I was curious about categorical y-axes too, because this is what I'd naively expect in a mosaic plot. However, your code doesn't work anymore on a fresh install of ggmosaic from CRAN.
@haleyjeppson: Where can I read more about the rationale for a continuous y-scale showing proportions only? Thanks!
It works with the latest version installed with devtools::install_package("haleyjeppson/ggmosaic")
. It seems that scale_y_product
has been replaced with scale_y_productlist
, which now handles all the breaking and labeling by itself:
library(tidyverse)
library(ggmosaic)
# Use scale_y_productlist() with default settings
ggplot(data=happy) +
geom_mosaic(aes(x=product(sex, marital), fill=sex)) +
scale_y_productlist()
# Add labels manually
ggplot(data=happy) +
geom_mosaic(aes(x=product(sex, marital), fill=sex)) +
scale_y_productlist(labels = c("Male", "Female"))
Thanks @andrewheiss , just what I was looking for.
Is there a way to reverse the order of the categories on the y axis, i.e. male then female? Changing the order in the data file didn't work and scale_y_productlist(trans = "reverse") is not supported.
Answer to my question:
I renamed the categories of my y-variable to be numerical, with the category I wanted at the top as the highest number. In the example above, if I wanted 'male' to be above 'female', I would replace 'male' with '2' and 'female' with '1' (or create a dummy variable with these codes in).
The best way to handle category ordering is to use an ordered factor. As with ggplot's other geoms, if a categorical variable is not explicitly ordered, the order is determined alphabetically. ggmosaic also seems to plot category orders in reverse, so you have to reverse the factor levels too.
library(tidyverse)
library(ggmosaic)
happy <- happy %>%
mutate(sex = factor(sex, levels=c("female", "male"), ordered=TRUE))
# Verify ordering
str(happy$sex)
#> Ord.factor w/ 2 levels "female"<"male": 1 2 1 1 1 2 2 2 1 1 ...
# Use scale_y_productlist() with default settings
ggplot(data=happy) +
geom_mosaic(aes(x=product(sex, marital), fill=sex)) +
scale_y_productlist()
I think we arrived at the same conclusion at the same time!
Do you know if there's a way to emulate expand = c(0, 0)
, to remove the padding between the mosaic and the labels?
I currently have this:
I've been tinkering with this and I couldn't find a way. geom_mosaic
doesn't seem to do anything with expand()
Thanks for your tinkering. That's a shame! I might have to use the old mosaic() function after all. Should I add this as a new issue?
I added this issue and included a workaround using coord_cartesian in #16.
See table for compatibility of ggplot2 and ggmosaic warnings in README
I'm so glad you're revamping productplots!
In the original
productplots::prodplot
function, the y-axis scale was categorical, not continuous, with breaks for each of the categories:With
ggmosaic::geom_mosaic
, the y-axis is now continuous, ranging from 0–1. The only way to distinguish between categories along the y axis is to use a fill aesthetic, since there are no y-axis labels:It would be nice to be able to use category labels on the y-axis in place of the continuous scale. Is there an option in
geom_mosaic()
that I'm missing to enable this, or is there some way of manipulatingscale_y_discrete()
to do this? Or is there a way to incorporate this feature fromprodplot
?Thanks!