hi folks, this is driving me up the wall. Apologies for posting twice in the same week, I'm writing up a thesis. I wish to color-code some dots in an xy plot. I've got a csv file with various elements, one of which is the color-key (with the header 'color'). If the color-key is decimal (eg. 1,2,3) then I can use plot (X ~ Y, col=data$color) The problem, however, is that using decimal numbers I can only produce 8 colors. It starts to recyle them after that (so, if possible values of my color column are 1,2,3,4,5,6,7,8,9,10,11, the values 1 and 9 both produce red, 2 and 10 produce black). However, I knew I could get more colors by using hexadecimal (tested with the legend) So, I carefully produced a csv file with hexadecimal values instead of decimal ones (eg. elements in the column are #ffffff, #ff0000) but if I use col=data$color it doesn't work, the entire plot is white. If I use these set of hexadecimal values in the legend, it works fine and I get lovely colors. I could use pch, but I'm already using symbols for another key. Grrr. What am I missing? Thanks! -- View this message in context: http://www.nabble.com/color-code-from-csv-tp19803835p19803835.html Sent from the R help mailing list archive at Nabble.com.
Hi, I don't know how to get R to use the hex codes, but as an alternative strategy you can use colors() to get a (much longer than 8) list of all the colors that R "knows", and use that with your index: plot(x, y, col=colors()[datacolor]) It takes a bit of fiddling to get distinguishable colors that you like, but not too hard. Conveniently, there's a color chart here: http://research.stowers-institute.org/efg/R/Color/Chart/ You can also try rainbow() and it's relatives (describe in ?rainbow). Sarah On Fri, Oct 3, 2008 at 3:10 PM, kerfuffle <pswi at ceh.ac.uk> wrote:> > hi folks, > > this is driving me up the wall. Apologies for posting twice in the same > week, I'm writing up a thesis. I wish to color-code some dots in an xy > plot. I've got a csv file with various elements, one of which is the > color-key (with the header 'color'). If the color-key is decimal (eg. > 1,2,3) then I can use > plot (X ~ Y, col=data$color) > The problem, however, is that using decimal numbers I can only produce 8 > colors. It starts to recyle them after that (so, if possible values of my > color column are 1,2,3,4,5,6,7,8,9,10,11, the values 1 and 9 both produce > red, 2 and 10 produce black). However, I knew I could get more colors by > using hexadecimal (tested with the legend) So, I carefully produced a csv > file with hexadecimal values instead of decimal ones (eg. elements in the > column are #ffffff, #ff0000) but if I use col=data$color it doesn't work, > the entire plot is white. If I use these set of hexadecimal values in the > legend, it works fine and I get lovely colors. > > I could use pch, but I'm already using symbols for another key. > > Grrr. What am I missing? > > Thanks!-- Sarah Goslee http://www.functionaldiversity.org
On Fri, Oct 3, 2008 at 2:10 PM, kerfuffle <pswi at ceh.ac.uk> wrote:> > hi folks, > > this is driving me up the wall. Apologies for posting twice in the same > week, I'm writing up a thesis. I wish to color-code some dots in an xy > plot. I've got a csv file with various elements, one of which is the > color-key (with the header 'color'). If the color-key is decimal (eg. > 1,2,3) then I can use > plot (X ~ Y, col=data$color)You might want to try using ggplot2 instead: install.packages("ggplot2") library(ggplot2) qplot(Y, X, data=data, colour = color) That will automatically pick a nice colour scale for you and draw a legend. You can see some examples of http://had.co.nz/ggplot2/geom_point.html Hadley -- http://had.co.nz/
So you have two problems: (1) integers to colors (2) hexadecimal values to colors They are quite different problems, I think. For the first one, you have to look at ?palette to know how integers are mapped to colors via the palette; and the second problem seems to be that 'read.csv()' has treated '#' as the comment character!! So actually the column has NOT been read in your 'data'. (print data$color in your console to see if my guess is correct) Hope this will make you come down from the wall :-) BTW, I think Hadley's 'ggplot' package will be more natural for you, as palette() in 'grDevices' has only 8 colors by default. Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086 Mobile: +86-15810805877 Homepage: http://www.yihui.name School of Statistics, Room 1037, Mingde Main Building, Renmin University of China, Beijing, 100872, China On Sat, Oct 4, 2008 at 3:10 AM, kerfuffle <pswi at ceh.ac.uk> wrote:> > hi folks, > > this is driving me up the wall. Apologies for posting twice in the same > week, I'm writing up a thesis. I wish to color-code some dots in an xy > plot. I've got a csv file with various elements, one of which is the > color-key (with the header 'color'). If the color-key is decimal (eg. > 1,2,3) then I can use > plot (X ~ Y, col=data$color) > The problem, however, is that using decimal numbers I can only produce 8 > colors. It starts to recyle them after that (so, if possible values of my > color column are 1,2,3,4,5,6,7,8,9,10,11, the values 1 and 9 both produce > red, 2 and 10 produce black). However, I knew I could get more colors by > using hexadecimal (tested with the legend) So, I carefully produced a csv > file with hexadecimal values instead of decimal ones (eg. elements in the > column are #ffffff, #ff0000) but if I use col=data$color it doesn't work, > the entire plot is white. If I use these set of hexadecimal values in the > legend, it works fine and I get lovely colors. > > I could use pch, but I'm already using symbols for another key. > > Grrr. What am I missing? > > Thanks! > -- > View this message in context: http://www.nabble.com/color-code-from-csv-tp19803835p19803835.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. >
kerfuffle wrote:> hi folks, > > this is driving me up the wall. Apologies for posting twice in the same > week, I'm writing up a thesis. I wish to color-code some dots in an xy > plot. I've got a csv file with various elements, one of which is the > color-key (with the header 'color'). If the color-key is decimal (eg. > 1,2,3) then I can use > plot (X ~ Y, col=data$color) > The problem, however, is that using decimal numbers I can only produce 8 > colors. It starts to recyle them after that (so, if possible values of my > color column are 1,2,3,4,5,6,7,8,9,10,11, the values 1 and 9 both produce > red, 2 and 10 produce black). However, I knew I could get more colors by > using hexadecimal (tested with the legend) So, I carefully produced a csv > file with hexadecimal values instead of decimal ones (eg. elements in the > column are #ffffff, #ff0000) but if I use col=data$color it doesn't work, > the entire plot is white. If I use these set of hexadecimal values in the > legend, it works fine and I get lovely colors. > >Hi kerfuffle, You might want to look at color.scale in the plotrix package that transforms numeric values into colors as well as the rainbow function that Sarah mentioned. Jim
kerfuffle wrote:> hi folks, > > this is driving me up the wall. Apologies for posting twice in the same > week, I'm writing up a thesis. I wish to color-code some dots in an xy > plot. I've got a csv file with various elements, one of which is the > color-key (with the header 'color'). If the color-key is decimal (eg. > 1,2,3) then I can use > plot (X ~ Y, col=data$color) > The problem, however, is that using decimal numbers I can only produce 8 > colors. It starts to recyle them after that (so, if possible values of my > color column are 1,2,3,4,5,6,7,8,9,10,11, the values 1 and 9 both produce > red, 2 and 10 produce black). However, I knew I could get more colors by > using hexadecimal (tested with the legend) So, I carefully produced a csv > file with hexadecimal values instead of decimal ones (eg. elements in the > column are #ffffff, #ff0000) but if I use col=data$color it doesn't work, > the entire plot is white. If I use these set of hexadecimal values in the > legend, it works fine and I get lovely colors. > > I could use pch, but I'm already using symbols for another key. > > Grrr. What am I missing? > > Thanks! >Could you provide a small code sample that reproduces your problem (like stated in the posting guide)? And also include the info from sessionInfo(). cheers, Paul -- Drs. Paul Hiemstra Department of Physical Geography Faculty of Geosciences University of Utrecht Heidelberglaan 2 P.O. Box 80.115 3508 TC Utrecht Phone: +31302535773 Fax: +31302531145 http://intamap.geo.uu.nl/~paul