stat_weighted_mean()
stat_weighted_mean()
computes mean value of
y (taking into account any weight
aesthetic if provided) for each value of x. More
precisely, it will return a new data frame with one line per unique
value of x with the following new variables:
Let’s take an example. The following plot shows all tips received according to the day of the week.
To plot their mean value per day, simply use
stat_weighted_mean()
.
We can specify the geometry we want using geom
argument.
Note that for lines, we need to specify the group
aesthetic as well.
An alternative is to specify the statistic in
ggplot2::geom_line()
.
Of course, it could be use with other geometries. Here a bar plot.
p <- ggplot(tips) +
aes(x = day, y = tip, fill = sex) +
stat_weighted_mean(geom = "bar", position = "dodge") +
ylab("mean tip")
p
It is very easy to add facets. In that case, computation will be done separately for each facet.
stat_weighted_mean()
could be also used for computing
proportions as a proportion is technically a mean of binary values (0 or
1).
ggplot(tips) +
aes(x = day, y = as.integer(smoker == "Yes"), fill = sex) +
stat_weighted_mean(geom = "bar", position = "dodge") +
scale_y_continuous(labels = scales::percent) +
ylab("proportion of smoker")
Finally, you can use the weight aesthetic to indicate weights to take into account for computing means / proportions.