Рассмотрим следующую ситуацию, когда у вас есть 800 против 200 наблюдений:
library(ggplot2)
df <- data.frame(
x = rnorm(1000, rep(c(1, 2), c(800, 200))),
class = rep(c("A", "B"), c(800, 200))
)
ggplot(df, aes(x, fill = class)) +
geom_histogram(bins = 20, position = "identity", alpha = 0.5,
# Note that y = stat(count) is the default behaviour
mapping = aes(y = stat(count)))
Вы можете масштабировать счет для каждой группы до максимума 1 с помощью y = stat(ncount)
:
ggplot(df, aes(x, fill = class)) +
geom_histogram(bins = 20, position = "identity", alpha = 0.5,
mapping = aes(y = stat(ncount)))
В качестве альтернативы, вы можете установить y = stat(density)
, чтобы общая площадь интегрировалась в 1.
ggplot(df, aes(x, fill = class)) +
geom_histogram(bins = 20, position = "identity", alpha = 0.5,
mapping = aes(y = stat(density)))
Обратите внимание, что после ggplot 3.3.0 stat()
вероятно будет заменено на after_stat()
.