But both syntaxes also create a distracting, silly legend.
(1) How can I suppress the legend? But, more important ...
(2) Legend suppression is a workaround. The creation of the legend in the first place suggests that I have not approached the problem correctly. Is there a way to make this plot that does *not* require a workaround?
(3) If there is to be a legend at all, it should say something like "data", "interpolation", "least squares line" and should show in turn a red dot (no line through it), a green line (without dot), and a blue line (without dot). How would one elegantly do that in ggplot2?
# One syntax:
set.seed(7)
DUT<-data.frame(eks=rnorm(9))
DUT$why<-DUT$eks * 0.3 + rnorm(9)/5
pointlayer<-layer(data=DUT, mapping=aes(x=eks, y=why, color="red"), geom="point", geom_params=list(shape=1))
linelayer<-layer(data=DUT, mapping=aes(x=eks, y=why, color="blue"), geom="line")
smoothlayer<- layer(data=DUT, mapping=aes(x=eks, y=why, color="green"), geom="smooth", method="lm", se=FALSE)
ggplot() + pointlayer + linelayer + smoothlayer
# Another syntax:
DUT$pointcolor<- "red"
DUT$linecolor<-"blue"
DUT$smoothcolor<-"green"
pointlayer2<-layer(data=DUT, mapping=aes(x=eks, y=why, color=pointcolor), geom="point", geom_params=list(shape=1))
linelayer2<-layer(data=DUT, mapping=aes(x=eks, y=why, color=linecolor), geom="line")
smoothlayer2<- layer(data=DUT, mapping=aes(x=eks, y=why, color=smoothcolor), geom="smooth", method="lm", se=FALSE)
ggplot() + pointlayer2 + linelayer2 + smoothlayer2
Thanks
Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A.
URL: http://www.people.vcu.edu/~jwegelin
ggplot() + pointlayer2 + linelayer2 + smoothlayer2 + opts(legend.position="none")
> --
> 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
>
In general the three layers might be anything. For instance, one layer might be a map of Europe; another might be a scatterplot of random numbers; the other might be a boxplot of the price of tea in China during the 19th century. Or it could be some arbitrary graphic that has yet to be invented. I am trying to learn on a deeper level, and exploit, the general structure of ggplot2.
pointlayer<-layer(data=DUT, mapping=aes(x=eks, y=why), color="pink",
geom="point", geom_params=list(shape=1, color="red"))
linelayer<-layer(data=DUT, mapping=aes(x=eks, y=why), geom="line",
geom_parames=list(color="blue"))
smoothlayer<- layer(data=DUT, mapping=aes(x=eks, y=why),
geom="smooth", geom_params=list(color="green"),
stat_params=list(method="lm", se=FALSE))
ggplot() + pointlayer + linelayer + smoothlayer
The main thing you seem to have been tripped up on is the difference
between setting an aesthetic to a fixed value versus mapping it to a
variable. To do the former, set it outside the aes() call (or inside
geom_parames or stat_parames lists as in my example above). To do the
latter, set it inside the aes() call (this will generate a legend
unless you turn it off).
Best,
Ista
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org