agent dunham
2012-Feb-15 14:35 UTC
[R] assign same legend colors than in the grouped data plot
Dear community, I've plotted data and coloured depending on the factor variable v3. In the legend, I'd like to assign properly the same colors than in the factor (the factor has 5 levels). I've been trying this but it doesn't work. plot(var1, var2, xlab = "var1", ylab = "var2", col =var3 , bty='L') legend(locator(1),c("level 1 var3", "level 2 var3","level 3 var3", "level 4 var3","level 5 var3"), col=var3,pch=16) Thanks in advance, crosspide at hotmail.com -- View this message in context: http://r.789695.n4.nabble.com/assign-same-legend-colors-than-in-the-grouped-data-plot-tp4390641p4390641.html Sent from the R help mailing list archive at Nabble.com.
R. Michael Weylandt
2012-Feb-16 02:27 UTC
[R] assign same legend colors than in the grouped data plot
This is certainly not a reproducible example, but this works fine for me. lets = factor(sample(letters[1:3], 10, TRUE)) plot(1:10, 1:10, col = lets) legend("topleft", legend = levels(lets), col = seq.int(length(lets)), lty = 1) Michael On Wed, Feb 15, 2012 at 9:35 AM, agent dunham <crosspide at hotmail.com> wrote:> Dear community, > > I've plotted ?data and coloured depending on the factor variable v3. > In the legend, I'd like to assign properly ?the same colors than in the > factor (the factor has 5 levels). > > I've been trying this but it doesn't work. > > plot(var1, var2, xlab = "var1", ylab = "var2", col =var3 , bty='L') > legend(locator(1),c("level 1 var3", ?"level 2 var3","level 3 var3", ?"level > 4 var3","level 5 var3"), col=var3,pch=16) > > > Thanks in advance, crosspide at hotmail.com > > -- > View this message in context: http://r.789695.n4.nabble.com/assign-same-legend-colors-than-in-the-grouped-data-plot-tp4390641p4390641.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Jim Lemon
2012-Feb-16 09:41 UTC
[R] assign same legend colors than in the grouped data plot
On 02/16/2012 01:35 AM, agent dunham wrote:> Dear community, > > I've plotted data and coloured depending on the factor variable v3. > In the legend, I'd like to assign properly the same colors than in the > factor (the factor has 5 levels). > > I've been trying this but it doesn't work. > > plot(var1, var2, xlab = "var1", ylab = "var2", col =var3 , bty='L') > legend(locator(1),c("level 1 var3", "level 2 var3","level 3 var3", "level > 4 var3","level 5 var3"), col=var3,pch=16) > >Hi agent, At a guess, your problem is that you are getting the right colors in the wrong order. Your legend call should probably be: legend(locator(1),c("level 1 var3","level 2 var3","level 3 var3", "level 4 var3","level 5 var3"),col=as.numeric(levels(var3)),pch=16) Then, you may get the correct colors. Jim