Keep Gthe Current Color Scheme
Plotting with color in R is kind of like painting a room in your house: you lot accept to pick some colors. R has some default colors ready to go, merely it's simply natural to want to play around and endeavor some different combinations. In this mail service we'll look at some ways you can ascertain new color palettes for plotting in R.
To begin, permit's employ the palette role to run into what colors are currently available:
palette() [ane] "blackness" "red" "green3" "blue" "cyan" "magenta" "yellow" "gray"
We have 8 colors currently in the palette. That doesn't hateful we can't apply other colors. It only means these are the colors nosotros can refer to by position. "blackness" is the first color, then the argument col=1 will return blackness. Besides, col=2 produces "scarlet" and then on. Let'southward demonstrate by plotting 8 dots with the 8 different colors. Setting cex=3 makes the dots iii times their normal size and pch=19 makes solid dots instead of the default open circles:
plot(one:viii, 1:8, col=1:8, pch=19, cex=3, xlab="", ylab="")
The palette function can also exist used to change the color palette. For example we could add "purple" and "brown". Below we offset save the current color palette to an object called cc, and so employ the c() function to concatenate cc with regal and brown:
cc <- palette() palette(c(cc,"purple","dark-brown")) palette() [ane] "black" "red" "green3" "blueish" "cyan" "magenta" "yellow" "gray" "purple" "chocolate-brown"
If we desire to revert back to the default palette, nosotros can call palette with the keyword "default":
How do we know what colors are available for our palette? We can use the colors part to come across. Try it! Information technology volition list all 657 colors. Below nosotros show the get-go 20:
length(colors()) # 657 colors [1] 657 colors()[1:20] [1] "white" "aliceblue" "antiquewhite" "antiquewhite1" "antiquewhite2" "antiquewhite3" "antiquewhite4" [8] "aquamarine" "aquamarine1" "aquamarine2" "aquamarine3" "aquamarine4" "azure" "azure1" [15] "azure2" "azure3" "azure4" "beige" "bisque" "bisque1"
Nosotros can use these colors past name if we like. For example, hither'south a scatterplot of the cars data that come up with R using the color "aquamarine3":
plot(dist ~ speed, information=cars, col="aquamarine3", pch=nineteen)
The Stowers Institute for Medical Research provides a handy chart that shows all available R colors: http://inquiry.stowers.org/mcm/efg/R/Color/Chart/ColorChart.pdf
Trying to choose practiced colors out of 657 choices can be overwhelming and lead to a lot of trial and fault. Fortunately a smashing deal of inquiry has been done on plotting and colour combinations and there are several tried-and-tested colour palettes to choose from. 1 R package that provides some of these palettes is RColorBrewer. Named for the creator of these color schemes, Cynthia Brewer, the RColorBrewer package makes it piece of cake to quickly load sensible color palettes.
The RColorBrewer package does not come with R and needs to be installed if you don't already take it. Once loaded, information technology provides functions for viewing and creating colour palettes.
# install.packages("RColorBrewer") library(RColorBrewer) RColorBrewer provides three types of palettes: sequential, diverging and qualitative.
- Sequential palettes are suited to ordered information that progress from low to high.
- Diverging palettes are suited to centered data with extremes in either direction.
- Qualitative palettes are suited to nominal or categorical information.
The available palettes are listed in the documentation. Even so the display.brewer.all office will plot all of them along with their name. In the graph below we see the sequential palettes, then the qualitative palettes, and finally the diverging palettes.
To create a RColorBrewer palette, use the brewer.pal function. It takes two arguments: n, the number of colors in the palette; and name, the name of the palette. Permit'due south make a palette of 8 colors from the qualitative palette, "Set2".
brewer.pal(northward = 8, proper name = "Set2") [i] "#66C2A5" "#FC8D62" "#8DA0CB" "#E78AC3" "#A6D854" "#FFD92F" "#E5C494" "#B3B3B3" palette(brewer.pal(n = eight, proper name = "Set2"))
Find the brewer.pal office by itself just displays the palette. Besides notice the colors are expressed in "hexadecimal triplets" instead of color names. To load the palette nosotros needed to use the palette role. These are at present the colors R volition use when referencing color by number. For example:
plot(dist ~ speed, data=cars, pch=19, col=2)
What about ggplot2? Changing color palettes works differently for ggplot2. Allow's brand a quick plot in ggplot using the iris data that come with R and see what the default colors expect like.
# install.packages("ggplot2") library(ggplot2) ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species)) + geom_point()
Conspicuously these are not the colors in our current color palette. It turns out ggplot generates its own color palettes depending on the scale of the variable that colour is mapped to. In the to a higher place example, colour is mapped to a discrete variable, Species, that takes 3 values. We would phone call this a qualitative palette and information technology works well for these data. Let's map color to a continuous variable, Sepal.Width:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Sepal.Width)) + geom_point()
Detect the palette changed to a bluish palette that gets progressively lighter as values increase. This is really a shine gradient between two shades of blue.
To modify these palettes nosotros utilise one of the scale_color functions that come with ggplot2. For example to use the RColorBrewer palette "Set2", we employ the scale_color_brewer role, like then:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, colour=Species)) + geom_point() + scale_color_brewer(palette = "Set2")
To change the smooth gradient color palette, nosotros utilise the scale_color_gradient with low and high color values. For instance, we tin set the low value to white and the high value to cherry:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Sepal.Width)) + geom_point() + scale_color_gradient(low = "white", high = "red")
Now what if in that location's a colour palette in ggplot that we would like to utilize in base R graphics? How tin can we figure out what those colors are? For example, let's say we like ggplot'south red, light-green, and blue colors information technology used in the offset plot above. They're non simply "reddish", "green" and "blue". They're a bit lighter and softer.
It turns out ggplot automatically generates discrete colors by automatically picking evenly spaced hues around something called the hcl color wheel. If a colour is mapped to a variable with two groups, the colors for those groups will come from opposite sides of the color wheel, or 180 degrees apart (360/ii = 180). If a color is mapped to a variable with 3 groups, the colors will come from 3 evenly spaced points around the wheel, or 120 degrees apart (360/3 = 120). Then on.
Looking at the documentation for the scale_color_discrete function tells us where on the hcl color wheel ggplot starts picking the colour: 15. This known every bit the h value, which stands for hue. The c and l values, which stand for chroma and luminance, are set up to 100 and 65. For iii groups, this means the h value are 15, 135 (15 + 120), and 255 (15 + 120 + 120). Now we tin utilise the hcl role that comes with R to go the associated hexadecimal triplets:
hcl(h = c(xv,135,255), c = 100, l = 65) [i] "#F8766D" "#00BA38" "#619CFF"
And we can use the palette function to add these colors to the color palette:
palette(hcl(h = c(15,135,255), c = 100, l = 65))
At present we can make a base R plot with ggplot2 colors. For example, here's the scatterplot role from the automobile packet plotting the iris data with ggplot2 colors.
# install.packages("car") library(car) scatterplot(Petal.Length ~ Sepal.Length | Species, information=iris)
Finally, it's relatively straight frontward to write a part to generate ggplot2 colors based on the number of groups. Below nosotros first make up one's mind the distance between points by dividing 360 by thou, the number of groups. Next we determine the actual points on the circle past starting with xv and cumulatively calculation the altitude. Finally we call the hcl function to get our colors. Of course the function could exist made more robust by allowing the c and l values and the starting point on the color wheel to be varied. But this function works fine if you're happy with the default ggplot2 colors for discrete variables.
ggplotColors <- role(yard){ d <- 360/g h <- cumsum(c(xv, rep(d,yard - one))) hcl(h = h, c = 100, l = 65) } For questions or clarifications regarding this article, contact the UVA Library StatLab: statlab@virginia.edu
View the entire collection of UVA Library StatLab articles.
Dirt FordStatistical Inquiry Consultant
Academy of Virginia Library
June 10, 2016
Source: https://data.library.virginia.edu/setting-up-color-palettes-in-r/
0 Response to "Keep Gthe Current Color Scheme"
Post a Comment