Hi everyone. I have a vector like this: x c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2) I would like to associate a colour to each of these entry. For example, I could use /topo.colors(3)/ (since I have 3 groups). I know it is easy to do this with /if/else/ statements, but I would like to find a proper way to do this. Thanks in advance, Phil -- View this message in context: http://r.789695.n4.nabble.com/Colors-vector-based-on-group-tp4361425p4361425.html Sent from the R help mailing list archive at Nabble.com.
Hello,> I have a vector like this: > > x > c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2) > > I would like to associate a colour to each of these entry. For example, I > could use topo.colors(3) (since I have 3 groups). > > I know it is easy to do this with if/else statements, but I would like to > find a proper way to do this.Easy, after knowing how many colors are there, just use 'x' as an index vector: ncolrs <- length(unique(x)) colrs <- topo.colors(ncolrs) xcolrs <- colrs[x] Or, in one instruction, xcolrs2 <- topo.colors(length(unique(x)))[x] all.equal(xcolrs, xcolrs2) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Colors-vector-based-on-group-tp4361425p4361662.html Sent from the R help mailing list archive at Nabble.com.
On Feb 6, 2012, at 9:40 AM, Filoche wrote:> Hi everyone. > > I have a vector like this: > > x > c > (1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2 > ) > > I would like to associate a colour to each of these entry. For > example, I > could use /topo.colors(3)/ (since I have 3 groups). > > I know it is easy to do this with /if/else/ statements, but I would > like to > find a proper way to do this. >topo.colors(3)[c(1,2,3,1,2,3)] [1] "#4C00FFFF" "#00FF4DFF" "#FFFF00FF" "#4C00FFFF" "#00FF4DFF" "#FFFF00FF" -- David Winsemius, MD West Hartford, CT
Hi there. I should have thought about that myself (/shame). Thank you very much for your time. Phil -- View this message in context: http://r.789695.n4.nabble.com/Colors-vector-based-on-group-tp4361425p4361686.html Sent from the R help mailing list archive at Nabble.com.
On Mon, 6 Feb 2012, Filoche wrote:> Hi everyone. > > I have a vector like this: > > x > c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2) > > I would like to associate a colour to each of these entry. For example, I > could use /topo.colors(3)/ (since I have 3 groups). > > I know it is easy to do this with /if/else/ statements, but I would like to > find a proper way to do this.Simply use 'x' for subsetting, i.e., with your example topo.colors(3)[x]. See also the packages 'colorspace' or 'RColorBrewer' (amongst others) for different color palettes. hth, Z> Thanks in advance, > Phil > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Colors-vector-based-on-group-tp4361425p4361425.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. >