geom_hline in faceted plot

1,297 views
Skip to first unread message

Adi

unread,
Dec 26, 2011, 2:38:43 PM12/26/11
to ggp...@googlegroups.com
Dear all,

Why does the following return an error?

Thanks for your help,

Adi

library(reshape)
library(ggplot2)

myDf = expand.grid(x=seq(1,3), y=0, group=factor(c('A','B','C')), mean=0)
myDf$y = c(rnorm(3, mean=1), rnorm(3, mean=2), rnorm(3, mean=3))
myDf$mean = c(rep(1,3), rep(2,3), rep(3,3))

ggplot(myDf) + geom_point(aes(x=x,y=y)) + facet_grid(.~group) + geom_hline(yintercept=mean)

Dennis Murphy

unread,
Dec 26, 2011, 2:53:03 PM12/26/11
to ggp...@googlegroups.com
Hi:

You need to wrap an aes() around the yintercept, since it is a
variable (and hence an aesthetic of the plot). So using my previous
reply where gp replaces group as a variable name, this works:

ggplot(myDf) + geom_point(aes(x = x, y = y)) +
geom_hline(aes(yintercept=mean) ) +
facet_wrap(~ gp, scales = 'free', nrow = 1)

HTH,
Dennis

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

Adi

unread,
Dec 27, 2011, 2:09:20 AM12/27/11
to ggp...@googlegroups.com
Thanks Dennis!

Adi

Adi

unread,
Dec 27, 2011, 2:17:56 AM12/27/11
to ggp...@googlegroups.com
Dennis,

What would be the best way to request an upgrade of the 'geom_hline' documentation? It doesn't mention 'yintercept' in the function usage, nor in the allowed aesthetics.

Greetz,

Adi

Dennis Murphy

unread,
Dec 27, 2011, 3:13:24 AM12/27/11
to ggp...@googlegroups.com
It's coming in the soon-to-be-released version, where the help pages
have many more examples. In particular, Hadley wrote examples for the
geom_hline/vline help pages that specifically show the difference
between setting the intercept at a fixed value for all groups and
mapping it to different values in different groups. In the first case,
the intercept is a constant, in the second it is a variable. In
ggplot2-speak, constants are set but variables are mapped; variable
aesthetics (i.e., plot characteristics such as color, fill, shape or
linetype that take different values in different groups) go within an
aes() statement, those that are constant throughout are set inside a
geom_*() statement but outside aes().

geom_hline() uses yintercept = as an argument; geom_vline() uses
xintercept = instead. See http://had.co.nz/ggplot2/geom_hline.html and
http://had.co.nz/ggplot2/geom_vline.html. More generally, the page
http://had.co.nz/ggplot2/ contains an on-line help system for 0.8.9
which contains many worked examples (scroll towards the bottom of the
page to find them).

Dennis

Adi

unread,
Dec 27, 2011, 3:59:22 AM12/27/11
to ggp...@googlegroups.com
Dear Dennis,

Thanks for your detailed response.

Adi


>> > To unsubscribe: email ggplot...@googlegroups.com

Reply all
Reply to author
Forward
0 new messages