use of stat_summary with a factor

57 views
Skip to first unread message

Douglas Bates

unread,
Jan 27, 2011, 3:39:08 PM1/27/11
to ggplot2
I often use plots like the enclosed to describe the between- and
within-group variability in data. (The line joins the average
responses for each group). That plot was created with lattice as

data(Dyestuff, package="lme4")
dotplot(reorder(Batch, Yield) ~ Yield, Dyestuff,
ylab = "Batch", jitter.y = TRUE, pch = 21, aspect = 0.32,
xlab = "Yield of dyestuff (grams of standard color)",
type = c("p", "a"))

I tried to create a similar plot using ggplot2 and got as far as

qplot(reorder(Batch, Yield), Yield, data=Dyestuff,
geom="point", pch=21, xlab=NULL,
ylab="Yield of dyestuff (grams of standard color)") +
stat_summary(fun.y = mean, geom="line") + coord_flip()

but the line I was hoping for didn't materialize. Earlier I had used
position=position_jitter(w=0.1,h=0) in the call to qplot() but I
pulled it in case the jittered values were considered the x values
when grouping the y's. However, even without the jittering I seem to
be unable to add the line, perhaps because one scale is a factor.

Suggestions welcome.

simple-Dyestuffplot.pdf

fernando

unread,
Jan 27, 2011, 3:51:52 PM1/27/11
to Douglas Bates, ggplot2
Dear Prof Bates,

I think you are correct about the need for a continuous x scale:

qplot(x = reorder(Batch, Yield), y = Yield, data=Dyestuff,


geom="point", pch=21, xlab=NULL,
ylab="Yield of dyestuff (grams of standard color)") +

stat_summary( aes( x = as.numeric( reorder(Batch, Yield) ) ), fun.y =


mean, geom="line") +
coord_flip()

Hope this helps,
fernando

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
Douglas Bates
Enviado el: jueves, 27 de enero de 2011 21:39
Para: ggplot2
Asunto: use of stat_summary with a factor

Suggestions welcome.

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

Douglas Bates

unread,
Jan 27, 2011, 3:58:10 PM1/27/11
to Ista Zahn, ggplot2
Thank you (and thanks to fernando too).

On Thu, Jan 27, 2011 at 2:52 PM, Ista Zahn <iz...@psych.rochester.edu> wrote:
> Hi,
> Use group=1:
>
> stat_summary(fun.y = mean, geom="line", aes(group=1))
>
> Best,
> Ista

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

> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>

Ista Zahn

unread,
Jan 27, 2011, 3:52:05 PM1/27/11
to Douglas Bates, ggplot2
Hi,
Use group=1:

stat_summary(fun.y = mean, geom="line", aes(group=1))

Best,
Ista

On Thu, Jan 27, 2011 at 3:39 PM, Douglas Bates <ba...@stat.wisc.edu> wrote:

Reply all
Reply to author
Forward
0 new messages