if(require(dplyr) && require(tidyr)) {
# histogram
p0 <- mpg %>%
dplyr::filter(manufacturer %in% c("dodge", "ford", "toyota", "volkswagen")) %>%
ggplot(mapping = aes(x = manufacturer, y = cty))
p0 + geom_hist_()
## set position
#### default is "stack_"
p0 + geom_hist_(mapping = aes(fill = fl))
#### "dodge_"
p0 + geom_hist_(position = "dodge_",
mapping = aes(fill = fl))
#### "dodge2_"
p0 + geom_hist_(position = "dodge2_",
mapping = aes(fill = fl))
# bar chart
mpg %>%
ggplot(mapping = aes(x = drv, y = class)) +
geom_hist_(orientation = "y")
# scale.y as "group"
p <- iris %>%
tidyr::pivot_longer(cols = -Species,
names_to = "Outer sterile whorls",
values_to = "x") %>%
ggplot(mapping = aes(x = `Outer sterile whorls`,
y = x, fill = Species)) +
stat_hist_(scale.y = "group",
prop = 0.6,
alpha = 0.5)
p
# with density on the left
p + stat_density_(scale.y = "group",
prop = 0.6,
alpha = 0.5,
positive = FALSE)
########### only `x` or `y` is provided ###########
# that would be equivalent to call function
# `geom_histogram()` or `geom_bar()`
### histogram
diamonds %>%
dplyr::sample_n(500) %>%
ggplot(mapping = aes(x = price)) +
geom_hist_()
### bar chart
diamonds %>%
dplyr::sample_n(500) %>%
ggplot(mapping = aes(x = cut)) +
geom_hist_()
}
Run the code above in your browser using DataLab