Overlaying scatter plot and mean plot

384 views
Skip to first unread message

Troy S

unread,
Dec 16, 2010, 2:23:09 PM12/16/10
to ggplot2
Hi,

I have a basic mean and sd plot.

dfsumm <- data.frame(x=1:3, mean=c(4,5,7), sd=c(1,1,2))
ggplot(dfsumm, aes(x, mean))+
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd))+geom_point()

How do I overlay a jittered scattered plot using a 2nd color?

Example data frame:
df <- data.frame(t=c(1,1,1,2,2,2,3,3,3), val=c(3.9,4,4.1,4.9,5,5.1,6.9,7,7.1))

Troy

Erik Iverson

unread,
Dec 16, 2010, 3:02:26 PM12/16/10
to Troy S, ggplot2
Try adding the following to your plot:

geom_jitter(aes(t, val), data = df, colour = I("red"))

> --
> You received this message because you are subscribed to the ggplot2
> mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Troy S

unread,
Dec 16, 2010, 6:50:15 PM12/16/10
to Dennis Murphy, ggplot2
Thanks all for the ideas.  I was so close..........

On 16 December 2010 14:57, Dennis Murphy <djm...@gmail.com> wrote:
Hi:

In addition to Erik's answer, I prefer to rein in the jittering, something like the following:

# Your original code:

dfsumm <- data.frame(x=1:3, mean=c(4,5,7), sd=c(1,1,2))
ggplot(dfsumm, aes(x, mean))+
 geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width = 0.5)+geom_point()

# I tightened the width of the error bars in the previous line.
# Generated more data to illustrate my point about tightening the jitter...
df <- data.frame(tt = rep(1:3, each = 40),
           val = round(rnorm(120, m = rep(c(4, 5, 7), each = 40))))


ggplot(dfsumm, aes(x, mean))+
 geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width = 0.5, size = 1) +
 geom_point(size = 3) +
 geom_jitter(aes(tt, val), data = df, colour = I("red"),
                   position = position_jitter(width = 0.05))

I added the position argument to Erik's geom_jitter() suggestion to exert control over the extent of the jittering and played around with other arguments such as thickness of the error bars (size = 1) and size of the center points (size = 3 in geom_point()). This should give you some ideas how to customize your plot.

HTH,
Dennis

Adam_L...@keybank.com

unread,
Dec 16, 2010, 2:53:43 PM12/16/10
to Troy S, ggplot2

Use stat_summary to calculate the mean and sd from the data - if you do this, then plotting the data itself will be just a matter of adding geom_jitter().

It'd be even easier to just use geom_boxplot along with geom_jitter().


Adam Loveland
216-689-5409
1Key




Troy S <troysoc...@yahoo.com>
Sent by: ggp...@googlegroups.com

12/16/2010 02:23 PM

To
ggplot2 <ggp...@googlegroups.com>
cc
Subject
Overlaying scatter plot and mean plot


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options:
http://groups.google.com/group/ggplot2



This communication may contain privileged and/or confidential information. It
is intended solely for the use of the addressee. If you are not the intended
recipient, you are strictly prohibited from disclosing, copying, distributing
or using any of this information. If you received this communication in error,
please contact the sender immediately and destroy the material in its entirety,
whether electronic or hard copy. This communication may contain nonpublic personal
information about consumers subject to the restrictions of the 
Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose
such information for any purpose other than to provide the services for which
you are receiving the information.

127 Public Square, Cleveland, OH 44114



If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.

Adam_L...@keybank.com

unread,
Dec 16, 2010, 3:19:22 PM12/16/10
to Erik Iverson, ggplot2

Ah, so you need the, "data=" if you're resetting the dataset. I kept getting an error when I simply included the name of the new dataset. Wow.




Adam Loveland
216-689-5409
1Key




Erik Iverson <er...@ccbr.umn.edu>
Sent by: ggp...@googlegroups.com

12/16/2010 03:02 PM

To
Troy S <troysoc...@yahoo.com>
cc
ggplot2 <ggp...@googlegroups.com>
Subject
Re: Overlaying scatter plot and mean plot


Reply all
Reply to author
Forward
0 new messages