# generate artificial data with numeric x and y
set.seed(67821)
x <- 1:100
y <- rnorm(length(x), mean = 10)
group <- factor(rep(c("A", "B"), times = 50))
my.data <- data.frame(x, y, group)
# using automatically generated text labels
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_panel_counts()
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_panel_counts()
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts()
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts(label.x = "left", hstep = 0.06, vstep = 0)
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts(aes(label = after_stat(pc.label)))
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts(aes(label = after_stat(pc.label)), digits = 3)
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts(aes(label = after_stat(fr.label)))
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts(aes(label = after_stat(dec.label)))
# one of x or y can be a factor
# label.x or label.y along the factor can be set to "factor" together
# with the use of geom_text()
ggplot(mpg,
aes(factor(cyl), hwy)) +
stat_boxplot() +
stat_group_counts(geom = "text",
label.y = 10,
label.x = "factor") +
stat_panel_counts()
# Numeric values can be used to build labels with alternative formats
# Here with sprintf(), but paste() and format() also work.
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_panel_counts(aes(label = sprintf("%i observations",
after_stat(count)))) +
scale_y_continuous(expand = expansion(mult = c(0.05, 0.12)))
ggplot(mpg,
aes(factor(cyl), hwy)) +
stat_boxplot() +
stat_group_counts(geom = "text",
aes(label = sprintf("(%i)", after_stat(count))),
label.y = 10,
label.x = "factor")
ggplot(mpg,
aes(factor(cyl), hwy)) +
stat_boxplot() +
stat_group_counts(aes(label = sprintf("n[%i]~`=`~%i",
after_stat(x), after_stat(count))),
parse = TRUE,
geom = "text",
label.y = 10,
label.x = "factor") +
stat_panel_counts(aes(label = sprintf("sum(n[i])~`=`~%i",
after_stat(count))),
parse = TRUE)
# label position
ggplot(my.data, aes(y)) +
stat_panel_counts(label.x = "left") +
stat_density(alpha = 0.5)
ggplot(my.data, aes(y, colour = group)) +
stat_group_counts(label.y = "top") +
stat_density(aes(fill = group), alpha = 0.3)
# The numeric value can be used as a label as is
ggplot(mpg,
aes(factor(cyl), hwy)) +
stat_boxplot() +
stat_group_counts(geom = "text",
aes(label = after_stat(count)),
label.x = "factor",
label.y = 10) +
annotate(geom = "text", x = 0.55, y = 10, label = "n[i]~`=`", parse = TRUE)
# We use geom_debug() to see the computed values
gginnards.installed <- requireNamespace("gginnards", quietly = TRUE)
if (gginnards.installed) {
library(gginnards)
ggplot(my.data, aes(x, y)) +
geom_point() +
stat_panel_counts(geom = "debug")
}
if (gginnards.installed) {
ggplot(my.data, aes(x, y, colour = group)) +
geom_point() +
stat_group_counts(geom = "debug")
}
Run the code above in your browser using DataLab