plot each layer in its own color

138 views
Skip to first unread message

Jacob Wegelin

unread,
Feb 28, 2011, 12:52:18 PM2/28/11
to ggp...@googlegroups.com

Below are two different syntaxes that explicitly define three layers, each with its own color, and then plot them on top of each other.

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

Jacob Wegelin

unread,
Feb 28, 2011, 12:56:30 PM2/28/11
to ggp...@googlegroups.com

The following syntax answers question (1):

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
>

Scott Chamberlain

unread,
Feb 28, 2011, 12:58:46 PM2/28/11
to Jacob Wegelin, ggp...@googlegroups.com
Isn't this what you need? :

ggplot(DUT, aes(eks, why)) +
geom_point(colour = "red") +
geom_line(colour = "green") +
geom_smooth(method = "lm", se = F)

Jacob Wegelin

unread,
Feb 28, 2011, 1:07:19 PM2/28/11
to Scott Chamberlain, ggp...@googlegroups.com

Thank you. This creates a *specific plot*, but I want to explicitly define three different layers and then plot them in three different colors. The example I provided is merely an oversimplified "toy" by which I sought to abstract the problem.

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.

Ista Zahn

unread,
Feb 28, 2011, 1:09:27 PM2/28/11
to Scott Chamberlain, Jacob Wegelin, ggp...@googlegroups.com
If you want to keep using the verbose layer syntax, use

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

Dennis Murphy

unread,
Feb 28, 2011, 5:21:12 PM2/28/11
to Jacob Wegelin, ggp...@googlegroups.com
Hi:

This appears to get you 99% of the way there, I believe, including the legend:

# The 'shortcut way':
ggplot(data = DUT, aes(x = eks, y = why)) +
  geom_point(aes(colour = 'Data'), shape = 1) +
  geom_line(aes(colour = 'Interpolation')) +
  geom_smooth(aes(colour = 'Least squares line'), method = 'lm', se = FALSE) +
  labs(x = 'x', y = 'y', colour = 'Type') +
  scale_colour_manual(values = c('Data' = 'red', 'Interpolation' = 'blue',
                             'Least squares line' = 'green'))

# The layered way:
pointlayer<-layer(data=DUT, mapping=aes(x=eks, y=why, color = 'Data'),
                  geom="point", geom_params=list(shape=1))

linelayer<-layer(data=DUT, mapping=aes(x=eks, y=why, color = 'Interpolation'),
                 geom="line")

smoothlayer<- layer(data=DUT, mapping=aes(x=eks, y=why, color = 'Least squares line'),
                    geom="smooth", stat_params=list(method="lm", se=FALSE))

ggplot() + pointlayer + linelayer + smoothlayer +
  labs(x = 'x', y = 'y') +
  scale_color_manual(name = 'Type',
                     values = c('Data' = 'red', 'Interpolation' = 'blue',
                                'Least squares line' = 'green'))

You'll need a hack to pick and choose among geom types in the legend. The problem is that you use color for both points and lines, so what gets rendered in the legend is (go figure) points and lines. See p. 111 of Hadley's text. In base graphics, there is both an lty and pch argument to legend() - if you want one but not the other, one can be set to NULL in the appropriate location of the vector. There is no such analogue in ggplot2 AFAIK. Hadley could tell you if something is in the works on the development side, or perhaps someone can hack the legend code to add such a feature and post it here. Absent that, the legend glyphs are what they are, and the legend code is behaving as documented.

HTH,
Dennis
Reply all
Reply to author
Forward
0 new messages