You need to mention where wtd.mean() comes from (a search with package
sos came up empty, but it did mention the Hmisc package, which luckily
worked). After loading Hmisc, there is a potential conflict between
Hmisc::summarize and plyr::summarize: whichever one is higher in the
search path will be used in ddply(). This is why it's safer in plyr to
use the Commonwealth spelling summarise as a matter of course.
That said, your stat_summary() code uses fun.y = "mean", so it will
compute the unweighted mean. The path of least resistance is to take
your wm data frame and use it as the input data for a geom_point layer
as follows, given df:
library(Hmisc)
library(ggplot2)
library(plyr)
wm <- ddply(df, "category", summarise,
wmean=round(wtd.mean(value, weight, na.rm=TRUE), 2)
ggplot(df, aes(x = category, y = value)) +
geom_boxplot(width=0.6, colour = "#3366FF") +
geom_point(data = wm, aes(x = category, y = wmean),
shape = 23, size = 3, fill ="white")
# Another way is to add the weight variable as an aesthetic in stat_summary():
ggplot(df, aes(x = category, y = value)) +
geom_boxplot(width=0.6, colour = "#3366FF") +
stat_summary( fun.y ="wtd.mean", aes(weight = weight), geom ="point",
shape = 23, size = 3, fill ="white" )
# Using your code, you would have to have done this to get it to work:
ggplot(df, aes(x = category, y = value, weight = weight)) +
geom_boxplot(aes(weight = NULL), width=0.6, colour = "#3366FF") +
stat_summary( fun.y ="wtd.mean", geom ="point",
shape = 23, size = 3, fill ="white" )
Assuming you want the ordering of your boxplots to be one, two, three,
redefine the levels of df$category before running any of the above
code.
Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to
ggplot2+u...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>