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
We can specify the geometry we want using
Note that for lines, we need to specify the group
aesthetic as well.
An alternative is to specify the statistic in
Of course, it could be use with other geometries. Here a bar plot.
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
Finally, you can use the weight aesthetic to indicate weights to take into account for computing means / proportions.