Order of geom_point and geom_line can cause error

66 views
Skip to first unread message

John Kane

unread,
Mar 3, 2013, 10:37:12 AM3/3/13
to ggp...@googlegroups.com
There is probably some simple explanation for what I am doing but what?

Below are two equations  that I thought should be equivalent but p1 works and p2 does not.  The only difference that I can see is the order of the geom_point() and geom_line. Data supplied below.

library(ggplot2)

p1  <-   ggplot(dd1  , aes(factor2, mm, colour = "red")) + geom_point()+
            geom_line(aes(as.numeric(factor2)))

 p2  <-  ggplot(dd1  , aes(factor2, mm, colour = "red")) +
           geom_line(aes(as.numeric(factor2))) + geom_point()

## ------------------start data---------------------------------------##

dd1  <-  structure(list(factor1 = structure(c(2L, 2L, 2L, 2L), .Label = c("A",
"B", "C"), class = "factor"), factor2 = structure(1:4, .Label = c("1",
"2", "3", "4"), class = "factor"), variable = structure(c(1L,
1L, 1L, 1L), .Label = c("y1", "y2"), class = "factor"), mm = c(0.262932267965655,
3.52204900424676, 3.42169297639079, 2.99632823526601)), .Names = c("factor1",
"factor2", "variable", "mm"), row.names = c(9L, 11L, 13L, 15L
), class = "data.frame")

## ------------------end data ---------------------------------------##



Dennis Murphy

unread,
Mar 3, 2013, 12:19:10 PM3/3/13
to John Kane, ggp...@googlegroups.com
Hi John:

The cleaner way to go about this is as follows:

ggplot(dd1, aes(factor2, mm, colour = "red")) + geom_point() +
geom_line(aes(group = 1))

ggplot(dd1, aes(factor2, mm, colour = "red")) +
geom_line(aes(group = 1)) + geom_point()

The group = 1 construct is designed for plotting selected geoms (e.g.,
lines/paths) across the levels of a factor.

The problem is probably a bit easier to see if your code is
reformulated as follows:

ggplot(dd1, aes(y = mm, colour = "red")) + geom_point(aes(x = factor2)) +
geom_line(aes(x = as.numeric(factor2))) # OK
ggplot(dd1, aes(y = mm, colour = "red")) +
geom_line(aes(x = as.numeric(factor2))) +
geom_point(aes(x = factor2))
# Error: Discrete value supplied to continuous scale

In the second plot, the first layer establishes a numeric scale for
the x-axis, so when the second layer is imposed and the x-values are
factor levels, ggplot2 barks because it is expecting a numeric object.
In the first plot call, the geom_point() layer sees that x is a
factor object so defines a discrete scale. When you coerce a factor
object to numeric to plot the lines, the coerced values can be mapped
to the discrete scale, so there is no conflict in terms of training
the two scales. It works in this particular case, but I wouldn't
regard it as a safe practice.

There's probably a better explanation for this phenomenon, but I'll
leave it to the gurus to chime in.

As I mentioned at the outset, the safer way is to use group = 1 instead.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

John Kane

unread,
Mar 6, 2013, 2:09:27 PM3/6/13
to ggp...@googlegroups.com
I think I understand.  The group = 1 approach makes a lot more sense.

Thanks Dennis.
Reply all
Reply to author
Forward
0 new messages