How to set the drawing color in R language
Most people do not understand the knowledge points of this "R language drawing color setting" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "R language drawing color how to set" article.
Color 1 color name representation in R language
There are two main ways of color representation in R language, one is expressed by the name of color, such as blue, red, orange and so on. You should know that the color name in R language can be obtained by using colors () method. The following figure shows only 50 color values:
Library (scales) show_col (colors () [1:50], label=T) 2 color value representation
Another color representation is expressed numerically: that is, RGB's method, through the change of the three color channels of red (R), green (G) and blue (B) and their superposition to get a variety of colors You can use the rgb () method-set values for three colors (0-1): for example, rgb (1,0,0) indicates red, and when you set a transparent color, add another parameter: rgb (1,0,0,0.1). It can also be a value between 0 and 255. for example, the rgb () method sets the color (0255,0,0, max=255): rgb (255,0,0, max=255) also means red, and the transparent color: rgb (255,0,0,0.1, max=255). Another color is written in hexadecimal characters: # FF0000 represents red, and no two bits represent the value of RGB in turn
3 numerical representation
Colors can be represented by numbers 1, 2, 3, etc. These numbers point to the swatch palette color in R, for example, 1 represents the first color of the current palette, 2 represents the second color of the current palette, and so on. The colors in the default swatch are as follows:
> palette () [1] "black", "red", "green3", "blue", "cyan", "magenta", "yellow" [8] "gray" 4 colors
Fill the column chart with colors, all of which represent red, and fill them into the four columns of the column chart:
Barplot (1:4, col = c (2, "red", rgb (1,0,0), "# FF0000")) color matching
There are many colors, but it is really difficult to match a set of colors without any artistic cells. Fortunately, there is a package of RColorBrewer in R that has already matched the color swatches for us to use:
Continuous sequential, color gradient.
> display.brewer.all (type= "seq") > brewer.pal (9, "Blues") # take out the color [1] "# F7FBFF", # DEEBF7 "# C6DBEF", # 9ECAE1 "# 6BAED6", # 4292C6 "# 2171B5" [8] "# 08519C"# 08306B"
Extreme Diverging, which generates a dark color emphasizing both ends and a light color representing the middle color, which can be used to label outliers in the data.
> display.brewer.all (type= "div") # > brewer.pal (11, "RdGy") [1] "# 67001F"# B2182B"# D6604D"# F4A582", # FDDBC7 "# FFFFFF" # E0E0E0 "[8]" # BABABA "# 878787"# 4D4D4D"# 1A1A1A"
Type Qualitative to generate colors that are significantly different from each other, usually used to mark classification data.
> display.brewer.all (type= "qual") > brewer.pal (8 heroine Set3') # take out the color [1] "# 8DD3C7", # FFFFB3 "# BEBADA", # FB8072 "# 80B1D3", # FDB462 "# B3DE69" [8] "# FCCDE5" color drawing application
Share a piece of drawing code and draw a group scatter chart:
# generate a color vector, mark the color of each point palette (brewer.pal (8, "Set1")) plot (iris$Petal.Length, iris$Petal.With, col= iris$Species, ylab= "Petal.With", xlab = "Petal.Length", pch=c (15, 16J. 17) [iris$Species], main = "iris", cex=2) legend ("topleft", legend=levels (iris$Species), col=c (1Mague 2, 3), pch=c (15, 16)) these are the contents of this article on "how to set the color of R language drawing" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.