0
votes

I saw an example of plot function in R, in which the "col" parameter had a value which was a variable rather than a colour value (eg. "blue","red" etc). For example in the example below, the col parameter points to variable "Region", and the graph shows Year in X axis and Sales in Y axis, with the points colored for Region (third dimension). I checked the documentation for plot function and also the color parameter, but there was no indication that the col parameter can take a variable name rather than colour values. I would like to understand whether I am looking at the right documentation.

year = c(1990:1994)

sales = c(1:5)

region = c('East','East','West','North','South')

SalesData = data.frame(sales,year,region)   

with(SalesData,plot(year,sales,col=region))
3
See ?par and the section Color Specification. - nicola
@nicola you can copy an excerpt from the docs and post it as an answer. - Roman Luštrik
@nicola the color specification doesn't give any such information. Please read my comment below. - KurioZ7

3 Answers

2
votes

Although the most used function to generate plots in R is plot, you'll find the most useful information under the ?par documentation. In it, there is a Color Specification section that describes how you can set colors to your plot. Here an excerpt:

 Colors can be specified in several different ways. The simplest
 way is with a character string giving the color name (e.g.,
 ‘"red"’).  A list of the possible colors can be obtained with the
 function ‘colors’.  Alternatively, colors can be specified
 directly in terms of their RGB components with a string of the
 form ‘"#RRGGBB"’ where each of the pairs ‘RR’, ‘GG’, ‘BB’ consist
 of two hexadecimal digits giving a value in the range ‘00’ to
 ‘FF’.  Colors can also be specified by giving an index into a
 small table of colors, the ‘palette’: indices wrap round so with
 the default palette of size 8, ‘10’ is the same as ‘2’.  This
 provides compatibility with S.  Index ‘0’ corresponds to the
 background color.  Note that the palette (apart from ‘0’ which is
 per-device) is a per-session setting.

Note that in your case you are not passing a character to the col argument, but a factor that is basically an integer value. Furthermore, you can specify as many colors as the points you have to plot, so you can declare the color of each point.

Hope this helps.

0
votes

If you are making sorts of graphs, look up http://coleoguy.blogspot.de/2016/06/symbols-and-colors-in-r-pch-argument.html

There is a tool for selecting colors: http://colorbrewer2.org/

Just remember, when you enter a series of colors in HEX, make sure you use caps: e.g. col=c("#1B9E77","#D95F02","#7570B3","#666666")

-1
votes

you have to put "with(SalesData,plot(year,sales,col=SalesData$region))" instead of "with(SalesData,plot(year,sales,col=region))".