missing legend

5 views
Skip to first unread message

Alessandra Carioli

unread,
Mar 17, 2014, 8:28:29 AM3/17/14
to ggplot2
Dear ggplotters,

I am trying to realize plots with a legend explaining all 8 lines plotted in each sector of the plotting area:
I have 4 variables (Variable, Type, Year, and the value Moran.I) and I want all Type(=1:8) plotted in the same plot. Somehow the legend does not appear.

Any help on the issue is very much appreciated!

Ale

# Factor definition
MoranI$layer1 <- factor(MoranI$Type)

p <- ggplot(MoranI, aes(Year,Moran.I))

p <- p + geom_point(data=MoranI[which(MoranI$Type=="1"),],colour = "red",linetype=1, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="2"),],colour = "red",linetype=2, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="3"),],colour = "black",linetype=1, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="4"),],colour = "black",linetype=2, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="5"),],colour = "black",linetype=3, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="6"),],colour = "blue", linetype=1, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="7"),],colour = "blue", linetype=2, size = 1) +
geom_line(data=MoranI[which(MoranI$Type=="8"),],colour = "blue",linetype=3, size = 1) +
labs(title = "Moran's I",
x = "", y = "") +
scale_fill_manual(values=c("red", "red", "black", "black", "black", "blue", "blue", "blue"),
labels=c("First Order Queen", "Rook", "20km","50km","100km","5 Nearest Neigh.","10 Nearest Neigh.","15 Nearest Neigh.")) +
facet_wrap(~ layer2)
p

Ben Bond-Lamberty

unread,
Mar 17, 2014, 9:16:05 AM3/17/14
to Alessandra Carioli, ggplot2
Alessandra, if you can post a sample data set, people will be able to
give you a complete solution, but basically: you're going about this
wrong. You want to do something like

p <- ggplot(MoranI, aes(Year,Moran.I,color=Type)) +
geom_line()+scale_fill_manual(...) + facet_wrap(~layer2)

rather than have all those different geom_line calls on subsetted data.

Ben
> --
> --
> 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/d/optout.

Alessandra Carioli

unread,
Mar 17, 2014, 9:45:05 AM3/17/14
to ggplot2
Dear Ben,

I have tried your code but instead of independent lines I got some range-like vertical lines and a gradient legend, while what I am looking for is a discrete legend and separated lines for each Type (hope I am clear), which I can get through my code (though I am sure there are better ways to do it) but somehow I am missing something especially the legend.

here is the reproducible sample (just for 3 years)

MoranI<-

structure(list(Variable = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L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1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L), Year = c(1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L, 1981L, 1981L, 1981L, 1981L,
1981L, 1981L, 1981L, 1981L, 1982L, 1982L, 1982L, 1982L, 1982L,
1982L, 1982L, 1982L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L,
1983L, 1983L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L,
1981L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1982L,
1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1983L, 1981L,
1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1981L, 1982L, 1982L,
1982L, 1982L, 1982L, 1982L, 1982L, 1982L, 1983L, 1983L, 1983L,
1983L, 1983L, 1983L, 1983L, 1983L), Moran.I = c(0.540747, 0.5405797,
0.4078049, 0.4740631, 0.4276186, 0.5406657, 0.5182929, 0.4989062,
0.6101088, 0.6103528, 0.4715438, 0.5419492, 0.4881312, 0.6148927,
0.5903421, 0.5666177, 0.6377123, 0.6375148, 0.5010176, 0.5783965,
0.5322119, 0.6407922, 0.6166838, 0.5984601, 0.598504, 0.5992415,
0.4024683, 0.5471531, 0.4792703, 0.58756, 0.5550787, 0.5252822,
0.5578256, 0.5591557, 0.3509712, 0.5015923, 0.4330358, 0.5390097,
0.5089087, 0.4821029, 0.5760824, 0.5763108, 0.3745475, 0.5465101,
0.4699708, 0.5683651, 0.5377624, 0.5060175, 0.4768215, 0.4775433,
0.3871242, 0.4383429, 0.4185521, 0.4727756, 0.4548275, 0.4511318,
0.4294426, 0.4285272, 0.370357, 0.4120133, 0.4058914, 0.4263254,
0.4211144, 0.4193595, 0.5083086, 0.507985, 0.392797, 0.4857052,
0.4669879, 0.5140324, 0.498557, 0.4871792, 0.4513231, 0.4511127,
0.3047774, 0.3574651, 0.2904683, 0.43515, 0.4023516, 0.3741743,
0.4774014, 0.4789285, 0.3348088, 0.4003615, 0.3270682, 0.4695394,
0.442335, 0.4151291, 0.5011734, 0.5018312, 0.3634379, 0.4335531,
0.3779432, 0.4993507, 0.4714454, 0.4479305, 0.4485135, 0.4483828,
0.3447374, 0.3972635, 0.3676306, 0.4539734, 0.4294781, 0.4150817,
0.5154965, 0.5160379, 0.3796501, 0.4545183, 0.4054143, 0.5156972,
0.4861617, 0.4659199, 0.5134582, 0.513209, 0.3757226, 0.4646701,
0.4310052, 0.5216325, 0.4905977, 0.4744248, 0.5091002, 0.5089814,
0.3757812, 0.4812767, 0.4441467, 0.5086103, 0.5025268, 0.4913501,
0.5404257, 0.5392619, 0.4267821, 0.5180893, 0.4806941, 0.5504175,
0.5497379, 0.5342167, 0.5683916, 0.5683884, 0.460608, 0.5457562,
0.5055448, 0.5718389, 0.5537678, 0.5430672, 0.5336862, 0.5343647,
0.4126542, 0.506562, 0.4782011, 0.5370306, 0.5316042, 0.5159653,
0.5734869, 0.5718078, 0.4549132, 0.5426491, 0.5128078, 0.5739732,
0.5706151, 0.5512252, 0.5721305, 0.5731029, 0.4550776, 0.5591467,
0.5284094, 0.5903879, 0.5806748, 0.5701444, 0.4272265, 0.4286471,
0.3649326, 0.3945609, 0.3636469, 0.4361228, 0.4140842, 0.4069273,
0.3599879, 0.3588595, 0.3028254, 0.3408152, 0.3201302, 0.3622267,
0.3520416, 0.3460858, 0.3867019, 0.3872801, 0.2895109, 0.3567411,
0.3290815, 0.3918606, 0.375338, 0.3550779, 0.11415248, 0.11117191,
0.08310874, 0.1017057, 0.11570715, 0.11361162, 0.11730029, 0.1228593,
0.04670173, 0.04658113, 0.06033356, 0.04999245, 0.06905119, 0.07927554,
0.07376407, 0.07361102, 0.03971592, 0.04078971, 0.0303708, 0.0646501,
0.07627096, 0.08026136, 0.10005235, 0.09511502), layer = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L,
2L, 3L, 4L, 5L, 6L, 7L,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L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L), .Label = c("MAC", "TFR", "TFR15", "TFR20",
"TFR25", "TFR30", "TFR35", "TFR40", "TFR45"), class = "factor")), .Names = c("Variable",
"Type", "Year", "Moran.I", "layer", "layer1", "layer2"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 249L, 250L, 251L,
252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 260L, 261L, 262L,
263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L, 271L, 272L, 497L,
498L, 499L, 500L, 501L, 502L, 503L, 504L, 505L, 506L, 507L, 508L,
509L, 510L, 511L, 512L, 513L, 514L, 515L, 516L, 517L, 518L, 519L,
520L, 745L, 746L, 747L, 748L, 749L, 750L, 751L, 752L, 753L, 754L,
755L, 756L, 757L, 758L, 759L, 760L, 761L, 762L, 763L, 764L, 765L,
766L, 767L, 768L, 993L, 994L, 995L, 996L, 997L, 998L, 999L, 1000L,
1001L, 1002L, 1003L, 1004L, 1005L, 1006L, 1007L, 1008L, 1009L,
1010L, 1011L, 1012L, 1013L, 1014L, 1015L, 1016L, 1241L, 1242L,
1243L, 1244L, 1245L, 1246L, 1247L, 1248L, 1249L, 1250L, 1251L,
1252L, 1253L, 1254L, 1255L, 1256L, 1257L, 1258L, 1259L, 1260L,
1261L, 1262L, 1263L, 1264L, 1489L, 1490L, 1491L, 1492L, 1493L,
1494L, 1495L, 1496L, 1497L, 1498L, 1499L, 1500L, 1501L, 1502L,
1503L, 1504L, 1505L, 1506L, 1507L, 1508L, 1509L, 1510L, 1511L,
1512L, 1737L, 1738L, 1739L, 1740L, 1741L, 1742L, 1743L, 1744L,
1745L, 1746L, 1747L, 1748L, 1749L, 1750L, 1751L, 1752L, 1753L,
1754L, 1755L, 1756L, 1757L, 1758L, 1759L, 1760L, 1985L, 1986L,
1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 1995L,
1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L,
2005L, 2006L, 2007L, 2008L), class = "data.frame")

Ben Bond-Lamberty

unread,
Mar 17, 2014, 9:56:29 AM3/17/14
to Alessandra Carioli, ggplot2
Thanks for the data. "Type" is currently a numeric variable, but you
want it to be categorical, like so:

ggplot(MoranI,aes(Year,Moran.I,color=factor(Type)))+geom_line()+facet_grid(~layer2)

Ben


On Mon, Mar 17, 2014 at 9:45 AM, Alessandra Carioli

Alessandra Carioli

unread,
Mar 17, 2014, 10:59:50 AM3/17/14
to ggplot2
Works perfectly, thanks!
Reply all
Reply to author
Forward
0 new messages