[R] assign same legend colors than in the grouped data plot

R. Michael Weylandt michael.weylandt at gmail.com
Thu Feb 16 03:27:27 CET 2012


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.



More information about the R-help mailing list