geom_point, how to plot the sum of all points for the same element using ggplot syntax.?

231 views
Skip to first unread message

Pablo marin-garcia

unread,
Feb 1, 2013, 1:37:50 PM2/1/13
to ggplot2
Hello,

I have a set of elements with two values each, I want to plot the sum
of the values for each element. I know that I must use stat_sum(),
but I don't know yet how to use it. I know that it is a trivial
question but looking at

http://docs.ggplot2.org/0.9.3/geom_point.html
http://docs.ggplot2.org/0.9.3/stat_sum.html

does not help for me (due my limited knowledge of ggplot2). Could
someone be so kind of provide me with an example for this kind of
plot?


A reproducible example is here:
seed(1234)
a <- rep(letters[seq( from = 1, to = 10 )],2)
b <- as.factor(rep(c(rep(1,10),rep(2,10)),2))
c <- sort(rnorm(mean=10,sd=0.7,n=40))
d <- c(rep("set1",20), rep("set2", 20))
test <- data.frame(a,b,c,d)
test
str(test)
# ploting the two values because I don't know yet how to use stat_sum()
ggplot(data=test)+geom_point(aes(x=a, y=c))+facet_wrap(~ d,
scales='free_x')+theme(axis.text.x = element_text(angle=90,
vjust=0.5))


> test
a b c d
1 a 1 8.282620 set1
2 b 1 8.792361 set1
3 c 1 8.996060 set1
4 d 1 9.019109 set1
5 e 1 9.143176 set1
6 f 1 9.181912 set1
7 g 1 9.233689 set1
8 h 1 9.281759 set1
9 i 1 9.396200 set1
10 j 1 9.585462 set1
11 a 2 9.611895 set1
12 b 2 9.650710 set1
13 c 2 9.684096 set1
14 d 2 9.690544 set1
15 e 2 9.714397 set1
16 f 2 9.730615 set1
17 g 2 9.919336 set1
18 h 2 9.972034 set1
19 i 2 10.118430 set1
20 j 2 10.121863 set1
21 a 1 10.122222 set2
22 b 1 10.124027 set2
23 c 1 10.206344 set2
24 d 1 10.213873 set2
25 e 1 10.262236 set2
26 f 1 10.345095 set2
27 g 1 10.356549 set2
28 h 1 10.369448 set2
29 i 1 10.411919 set2
30 j 1 10.429783 set2
31 a 2 10.589949 set2
32 b 2 10.654281 set2
33 c 2 10.680950 set2
34 d 2 10.874659 set2
35 e 2 10.877735 set2
36 f 2 10.958514 set2
37 g 2 10.978281 set2
38 h 2 11.158989 set2
39 i 2 11.186938 set2
40 j 2 11.323160 set2

Thanks

--
- Pablo Marin-Garcia

Brandon Hurr

unread,
Feb 2, 2013, 10:03:12 AM2/2/13
to Pablo marin-garcia, ggplot2
Why not just add them in the aes()? 

ggplot(data = data, aes(x = A+B, y= C+D) + geom_point()





--
--
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.



Dennis Murphy

unread,
Feb 2, 2013, 2:40:49 PM2/2/13
to Pablo marin-garcia, ggplot2
Hi:

Is this what you had in mind?

library(plyr)
library(ggplot2)

tstsumm <- ddply(test, .(d, a), summarise, sumc = sum(c))
> head(tstsumm)
d a sumc
1 set1 a 18.09529
2 set1 b 18.42677
3 set1 c 18.76299
4 set1 d 18.82238
5 set1 e 18.97412
6 set1 f 19.49590

ggplot(tstsumm, aes(x = a, y = sumc)) + geom_point() + facet_wrap(~ d)

The plyr package is a useful companion to ggplot2 - it is often the
case that pre-processing data outside of ggplot2 with plyr or some
other data summarization package is simpler than trying to accomplish
the same task inside ggplot2. Moreover, it gives you a way of checking
whether or not the inputs are correct before passing them to ggplot().

Dennis

Pablo marin-garcia

unread,
Feb 3, 2013, 5:23:57 PM2/3/13
to Dennis Murphy, ggplot2
thanks Denis, my answer is inline...

On Sat, Feb 2, 2013 at 8:40 PM, Dennis Murphy <djm...@gmail.com> wrote:
> Hi:
>
> Is this what you had in mind?
>
> library(plyr)
> library(ggplot2)
>
> tstsumm <- ddply(test, .(d, a), summarise, sumc = sum(c))
[...]
> The plyr package is a useful companion to ggplot2 - it is often the
> case that pre-processing data outside of ggplot2 with plyr or some
> other data summarization package is simpler than trying to accomplish
> the same task inside ggplot2. Moreover, it gives you a way of checking
> whether or not the inputs are correct before passing them to ggplot().
>

I have already done it with ddply ( it is a fantastic tool that I use
a lot), but I wanted to know if indeed stat_sum was the way to go (as
an inplicit ddply inside ggplot) but I was missing something about how
to use it.

It is true that ddply gives you more granularity, but I want also to
understand better how things work in ggplot beyond the basics. I use a
lot ggplot but only 4 or 5 types of graphics. When I want to do
something more complicated every 6 months or so I always realise that
I have forgotten all the advanced things and I need to go again to the
ggplot book and googleing around.



Pablo
--
- Pablo Marin-Garcia
Reply all
Reply to author
Forward
0 new messages