geom_hline() with facets doesn't behave like in the example

745 views
Skip to first unread message

a11_msp

unread,
Apr 29, 2009, 5:10:28 AM4/29/09
to ggplot2
Hi,

sorry for flooding the forum with posts over the last several days -
just need to make quite a complex figure, for which ggplot2 seems to
be the library of choice...

I'm trying to reproduce the example from the website:

# To display different lines in different facets, you need to
# create a data frame.
p <- qplot(mpg, wt, data=mtcars, facets = vs ~ am)

hline.data <- data.frame(z = 1:4, vs = c(0,0,1,1), am = c(0,1,0,1))
p + geom_hline(aes(yintercept = z), hline.data)

This should result in lines with different intercepts depending on the
facet, but unfortunately on my machine they all have the intercept of
zero. Looks like something is wrong...

> sessionInfo()

R version 2.8.1 (2008-12-22)
i386-apple-darwin8.11.1

locale:
en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] grid stats graphics grDevices utils datasets
methods base

other attached packages:
[1] ggplot2_0.8.2 reshape_0.8.2 plyr_0.1.5 proto_0.3-8

loaded via a namespace (and not attached):
[1] RColorBrewer_1.0-2 tools_2.8.1

Many thanks
Mikhail

kevin

unread,
Jun 22, 2009, 11:03:34 AM6/22/09
to ggplot2
Hi,

I'm also having the same issue: I cannot get geom_hline or geom_vline
to work
correctly with facet grids. I tried creating a data frame to map the
facet
factors to the appropriate x or y intercepts, as in the example for
geom_hline
in the documentation, but always get lines at zero. I also tried
modifying the
original data frame to include these values, but the result is the
same.

Is this a bug - if so is there any way I can work around it? I have a
density
plot with three facets and need a different vertical line on each. Can
I
somehow directly select each facet individually and draw the lines on
them?

Thanks!
Kevin


On Apr 29, 10:10 am, a11_msp <absorbt...@gmail.com> wrote:
> Hi,
>
> sorry for flooding the forum with posts over the last several days -
> just need to make quite a complex figure, for which ggplot2 seems to
> be the library of choice...
>
> I'm trying to reproduce the example from the website:
>
> # To display different lines in different facets, you need to
> # create a data frame.
> p <- qplot(mpg, wt, data=mtcars, facets = vs ~ am)
>
> hline.data <- data.frame(z = 1:4, vs = c(0,0,1,1), am = c(0,1,0,1))
> p +geom_hline(aes(yintercept = z), hline.data)

hadley wickham

unread,
Jun 22, 2009, 11:17:29 AM6/22/09
to kevin, ggplot2
Can you please provide a reproducible example?
Hadley
--
http://had.co.nz/

kevin

unread,
Jun 22, 2009, 12:04:28 PM6/22/09
to ggplot2

The following produces a two facet plot with vertical lines at zero on
my machine:

df <- data.frame(
fac=c(rep('A', 100), rep('B', 100)),
var=c(rnorm(100, mean=0, sd=1), rnorm(100, mean=5, sd=1)),
thr=c(rep(2,100), rep(6, 100)))

p <- ggplot(df)
p <- p + geom_density(aes(x=var))
p <- p + geom_vline(aes(xintercept=thr))
p <- p + facet_grid(fac ~ .)
print(p)

The same happens if I separate the values for the vertical lines into
a separate data frame:

line.df <- data.frame(fac=c('A', 'B'), thr=c(2, 6))
# ...
p <- p + geom_vline(aes(xintercept=thr), data=line.df)

Same story with horizontal lines. Perhaps I'm doing something wrong?

Kevin

hadley wickham

unread,
Jun 22, 2009, 12:16:39 PM6/22/09
to kevin, ggplot2
On Mon, Jun 22, 2009 at 11:04 AM, kevin<kevin.mc...@gmail.com> wrote:
>
>
> The following produces a two facet plot with vertical lines at zero on
> my machine:
>
> df <- data.frame(
>        fac=c(rep('A', 100), rep('B', 100)),
>        var=c(rnorm(100, mean=0, sd=1), rnorm(100, mean=5, sd=1)),
>        thr=c(rep(2,100), rep(6, 100)))
>
> p <- ggplot(df)
> p <- p + geom_density(aes(x=var))
> p <- p + geom_vline(aes(xintercept=thr))
> p <- p + facet_grid(fac ~ .)
> print(p)

It looks fine to me, which suggests you probably don't have the latest
version of ggplot2.

Hadley

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

kevin

unread,
Jun 22, 2009, 3:21:30 PM6/22/09
to ggplot2

Thank you, the version was indeed the problem. I was using version
0.8.2 (R version 2.8.1); I upgraded to R 2.9 and ggplot2 0.8.3 and the
geom_hline and geom_vline now work fine with facets.

Fantastic package by the way.

Thanks again,
Kevin

On Jun 22, 5:16 pm, hadley wickham <h.wick...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages