stat_summary and size

19 views
Skip to first unread message

David Bess

unread,
Apr 22, 2014, 2:27:08 PM4/22/14
to ggp...@googlegroups.com

Greetings,

 

I am trying to create a graph where the y axis is some numeric data, the y axis is a factor group, and points represent the mean of the numeric data for each factor group. Further I need the size of the points to be scaled by the proportional number of observations in each group. This last part is where I keep getting stuck, even after hours of google searching and re-reading ggplot2 in its entirety.

 

Here is the code I have working...it uses the mtcars dataset which I think most people have available.


fig1 <- ggplot(mtcars, aes(cyl, mpg))

fig1 + stat_summary(fun.y = mean, geom = "point", size = 5,
aes(group = 1), color = "red")

 

The only thing that is missing is that I need the size to be scaled based on the proportional number of observations in each group.

 

Thanks in advance,

 

David

Ben Bond-Lamberty

unread,
Apr 22, 2014, 2:44:48 PM4/22/14
to David Bess, ggplot2
I'd probably pre-summarize the data, e.g. using the plyr package:

mtcars1 <- ddply(mtcars,.(cyl),summarise,mpg=mean(mpg),n=length(cyl))

...and then plot using mtcars1.

Ben
> --
> --
> 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/d/optout.
Reply all
Reply to author
Forward
0 new messages