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)
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
Thanks a lot.
-Premal
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