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