Summarise x values at every unique y

24 views
Skip to first unread message

Simon

unread,
Sep 1, 2009, 1:16:30 PM9/1/09
to ggplot2
I would like to display a line going through the means of x values for
every unique y value:
stat_summary gives me the opposite (as documented...) :
qplot(displ, reorder(manufacturer, displ), data = mpg)+stat_summary(aes
(group=1), fun.y=mean, geom="line", col=2)

ie, the equivalent of this:
qplot(reorder(manufacturer, displ), displ, data = mpg)+stat_summary(aes
(group=1), fun.y=mean, geom="line", col=2)+coord_flip()

except that for my real data I use a facet_grid (facet_grid(td~.,
scale="free", space="free", as.table=FALSE)) and the coord_flip seems
to mess it up...

How can I achieve this?

TIA

Simon.

Simon

unread,
Sep 2, 2009, 5:43:28 PM9/2/09
to ggplot2
I forgot to cc to the list an example with real data that Juliet
requested... so here it is:

Using the mpg dataset available in the ggplot2 package:
data(mpg)
# I would like to add to the following plot, lines, in each panel,
that go through the means of "displ" for each car
qplot(displ, reorder(model, displ), data = mpg)+facet_grid
(manufacturer~., space="free", scales="free_y")

# As is expect from the documentation, the following line computes
"column" means
# instead of the "row" means I'd like
qplot(displ, reorder(model, displ), data = mpg)+stat_summary(aes
(group=1), fun.y=mean, geom="line", col=2)+facet_grid(manufacturer~.,
space="free", scales="free_y")

# And if I try to rotate the data, this somehow seems to mess the
scales of the panels:
qplot(reorder(model, displ), displ, data = mpg)+stat_summary(aes
(group=1), fun.y=mean, geom="line", col=2)+facet_grid(manufacturer~.,
space="free", scales="free_x")+coord_flip()

So I am stumped...

Any Idea?

Thanks,

Simon.

hadley wickham

unread,
Sep 4, 2009, 2:18:49 PM9/4/09
to Simon, ggplot2
library(ggplot2)


Whenever the built in ggplot2 summaries can't do what you want, it's
usually best to start doing it yourself. You can compute the per
model displacement means as follows:

means <- ddply(mpg, c("manufacturer", "model"), summarise,
displ = mean(displ))

You can then add it to your plot as follows:

qplot(displ, reorder(model, displ), data = mpg) +
geom_point(data = means, colour = "red", size = 2) +
facet_grid(manufacturer~., space="free", scales="free_y")

Regards,

Hadley
--
http://had.co.nz/

Simon de Bernard

unread,
Sep 5, 2009, 12:22:48 PM9/5/09
to hadley wickham, ggplot2
Hi Hadley,

thanks for your answer, however my problem is not in computing and displaying the means, but with drawing lines through the means (see my examples)

Any idea?

Thanks,

Simon.

hadley wickham

unread,
Sep 5, 2009, 1:53:27 PM9/5/09
to Simon de Bernard, ggplot2
qplot(displ, reorder(model, displ), data = mpg) +
geom_vline(aes(xintercept = displ), data = means, colour = "red") +
facet_grid(manufacturer~., space="free", scales="free_y")

?

Hadley
--
http://had.co.nz/

Simon de Bernard

unread,
Sep 5, 2009, 4:38:45 PM9/5/09
to hadley wickham, ggplot2
Sorry I wasn't clear... when I said through the means I meant connecting the means...

Like this:

qplot(reorder(model, displ), displ, data = mpg)+stat_summary(aes 
(group=1), fun.y=mean, geom="line", col=2)+facet_grid(manufacturer~., 
space="free", scales="free_x")+coord_flip() 


except that facet_grid seems to disregard the 'scales="free_x"' parameter...

Simon

hadley wickham

unread,
Sep 6, 2009, 9:13:09 AM9/6/09
to Simon de Bernard, ggplot2
Oh, I finally get it - sorry for being so dense. Basically what you
want is 90 degree rotation of geom_line - you can get that with
coord_flip, but coordinate systems do not currently interact well with
free scales. An alternative is to use geom_path, which connects
points in the in order which they appear in the data:

library(ggplot2)

means <- ddply(mpg, c("manufacturer", "model"), summarise,
displ = mean(displ))

means <- means[with(means, order(manufacturer, displ)), ]

qplot(displ, reorder(model, displ), data = mpg) +
geom_path(aes(group = 1), data = means, colour = "red") +
facet_grid(manufacturer~., space="free", scales="free_y")

Regards,

Hadley

On Sat, Sep 5, 2009 at 3:38 PM, Simon de
--
http://had.co.nz/

Simon de Bernard

unread,
Sep 6, 2009, 11:06:39 AM9/6/09
to hadley wickham, ggplot2
That's it :-)

Thanks

Simon.
Reply all
Reply to author
Forward
0 new messages