# NOT RUN {
if(require(dplyr)) {
mpg %>%
dplyr::filter(drv != "f") %>%
ggplot(mapping = aes(x = drv, y = cty, fill = factor(cyl))) +
geom_density_(alpha = 0.1)
# only `x` or `y` is provided
# that would be equivalent to call function `geom_density()`
diamonds %>%
dplyr::sample_n(500) %>%
ggplot(mapping = aes(x = price)) +
geom_density_()
# density and boxplot
# set the density estimate on the left
mpg %>%
dplyr::filter(drv != "f") %>%
ggplot(mapping = aes(x = drv, y = cty,
fill = factor(cyl))) +
geom_density_(alpha = 0.1,
scale.y = "group",
as.mix = FALSE,
positive = FALSE) +
geom_boxplot()
# x as density
set.seed(12345)
suppressWarnings(
diamonds %>%
dplyr::sample_n(500) %>%
ggplot(mapping = aes(x = price, y = cut, fill = color)) +
geom_density_(orientation = "x", prop = 0.25,
position = "stack_",
scale.y = "group")
)
}
# settings of `scale.y` and `as.mix`
# }
# NOT RUN {
ggplots <- lapply(list(
list(scale.y = "data", as.mix = TRUE),
list(scale.y = "data", as.mix = FALSE),
list(scale.y = "group", as.mix = TRUE),
list(scale.y = "group", as.mix = FALSE)
),
function(vars) {
scale.y <- vars[["scale.y"]]
as.mix <- vars[["as.mix"]]
ggplot(mpg,
mapping = aes(x = drv, y = cty, fill = factor(cyl))) +
geom_density_(alpha = 0.1, scale.y = scale.y, as.mix = as.mix) +
labs(title = paste("scale.y =", scale.y),
subtitle = paste("as.mix =", as.mix))
})
suppressWarnings(
gridExtra::grid.arrange(grobs = ggplots)
)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab