Getting a legend with two layers

197 views
Skip to first unread message

Forafo San

unread,
Aug 3, 2010, 10:47:20 AM8/3/10
to ggp...@googlegroups.com
I have annual time-series data on the U.S. federal government's
receipts and outlays. I'm displaying the series on the same plot, one
as geom_area and the other as geom_line. The resulting plot looks very
nice. However, I'm not sure how to get a legend with two boxes one
with the whole box filled with the fill colour of the layer created
with geom_area and the other box containing a line of the size and the
colour that appear in the layer created with geom_line.

Any help is appreciated.
-Premal P. Vora

FYI the code:
---------------------------------
p<-ggplot(dat,aes(year,outlays))
p<- p + geom_area(fill="red")
p + geom_line(aes(y=receipts),size=1)

fernando

unread,
Aug 3, 2010, 4:34:51 PM8/3/10
to Forafo San, ggp...@googlegroups.com
Hi Premal,

I think one option is to restructure your data with melt and use then manual
scales.

Hope this helps,
fernando


dat <- data.frame(year = 1901:2000, outlays = rnorm(100), receipts =
rnorm(100))
dat <- melt(dat, id.vars = "year")
str(dat)
names(dat) <- c("year", "ind", "series")

p <- ggplot(dat, aes(x = year, y = series)) +
geom_area(aes(fill = ind)) +
geom_line(aes(colour = ind), size = I(1)) +
scale_fill_manual(name = "The same legend title", values =
c("outlays" = "red", "receipts" = NA)) +
scale_colour_manual(name = "The same legend title", values =
c("outlays" = NA, "receipts" = "blue"))
p


sessionInfo()
R version 2.11.1 (2010-05-31)
x86_64-pc-mingw32

locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252
[3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Spain.1252

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

other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3 plyr_1.1
foreign_0.8-40

loaded via a namespace (and not attached):
[1] digest_0.4.2 tools_2.11.1

-----Mensaje original-----
De: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] En nombre de
Forafo San
Enviado el: martes, 03 de agosto de 2010 16:47
Para: ggp...@googlegroups.com
Asunto: Getting a legend with two layers

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

Forafo San

unread,
Aug 4, 2010, 12:10:44 PM8/4/10
to fernando, ggp...@googlegroups.com
Fernando,
Your code works perfectly and I understand most of it except: in the
geom_area and the geom_line syntax that you wrote, how does ggplot
figure out that the area layer should plot only one series (and not
the other) and the line syntax should plot the other series (and not
the first). It's probably somewhere in Hadley's ggplot2 book, but if
you can provide a quick insight or reference, that would be immensely
helpful.

Thanks a lot.
-Premal

fernando

unread,
Aug 5, 2010, 4:17:44 AM8/5/10
to Forafo San, ggp...@googlegroups.com
Hi Forafo,

If you run the example line by line, you will note that indeed both
geom_area and geom_line produce the expected output, e.g. areas and lines
with different fills and colours for each level of the factor. The trick is
indeed in the manual scales, in which you can specify how each level will be
draw; by setting alternatively the fill or the colour to NA, you obtain the
desired effect of drawing only one of the geoms for each level of the
factor. Because you use the same 'name' for both scales, they are combined.

Hope this helps,
fernando

-----Mensaje original-----
De: Forafo San [mailto:ppv....@gmail.com]
Enviado el: miércoles, 04 de agosto de 2010 18:11
Para: fernando
CC: ggp...@googlegroups.com
Asunto: Re: Getting a legend with two layers

Reply all
Reply to author
Forward
0 new messages