I have a dataset that looks like
df <- data.frame(cbind( c(rep.int(x = 0, times =7), 1:3), c(1, 1, 1, 0, 1, 0, 1, 1, 0, 0), c(1:3, 1:3, 1:3, NA))) names(df) <- c("cars", "sex", "status") df$sex <- factor(df$sex, labels = c("male", "female")) df$status <- factor(df$status, labels = c("bad", "ok", "good")) df$car <- (df$cars > 0)
I would like to use ggplot2 to create faceted histograms with the following characteristics:
- Border by categorical variables (gender and status in this example)
- Each panel contains one bar per level of this factor (for example, men and women for "gender").
- Each bar shows how many percent of total observations for this level of this factor have at least 1 car (for example, the percentage of men with at least 1 car).
How can I do this seamlessly in ggplot2? (Or, alternatively, do you have a better suggestion on how to present these proportions on a graph?)
source share