What are several common color matching methods of ggplot2 graphics in R language?
This article is about R language ggplot2 graphics common color matching methods are what, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, say no more, follow the editor to have a look.
Examples show several commonly used ggplot color extraction methods, such as:
Distinguishing colors by strings
Color extraction by numbers
Take gradual discoloration
The color is selected by RColotBrewer scheme.
1. Construct dataset # Construct dataset df = data.frame (y=seq (1,20,2), z=seq (1000, 100,100), c=paste ("color", seq (1,10,1)) df
Running result:
two。 Install and load package install.packages ("ggplot2") # install package install.packages ("RColorBrewer") library (ggplot2) # load package library (RColorBrewer) 3. Ggplot (df, aes (x, y)) + geom_bar (stat= "identity")
Running result:
3.2 default Color
In ggplot2's graphics syntax, color is a graphic attribute, just like the position of x, the position of y, the size, and so on.
For geometric objects in a drawing, you can set the color by setting the value of the colour or fill parameter.
Generally speaking, the colour parameter controls the color of lines and polygon outlines, while the fill parameter controls the fill color of polygons.
If you need a variable (a column of the data box) to control the color of the geometric object, set the value of the colour or fi11 parameter to the column name of a column in the data.
Ggplot (df, aes (x, y, fill=c)) + geom_bar (stat= "identity") + labs (x = "x axis", y = "y axis", fill= "legend")
Running result:
3.3 Digital Color selection-positive sequence
The x variable is an incremental vector.
Ggplot (df, aes (x, y, fill=x)) + geom_bar (stat= "identity") + labs (x = "x axis", y = "y axis", fill= "legend")
Running result:
3.4 Digital Color selection-reverse order
The z variable is a decreasing vector.
Ggplot (df, aes (x, y, fill=z)) + geom_bar (stat= "identity") + labs (x = "x axis", y = "y axis", fill= "legend")
Running result:
3. 5 gradient: color 1-color 2 colors