How To Use 2 Different Colors In Plot In R
Colors for Plotting
The departure between a simple graph and a visually stunning graph is of course a matter of many features. Simply one of the biggest contributors to the "wow" factors that often accompanies R graphics is the careful employ of colour. By default, R graphs tend to be black-and-white and, in fact, rather unattractive. Merely R provides many functions for carefully controlling the colors that are used in plots. This tutorial looks at some of these functions.
To start, we demand to accept a baseline graph. We'll use a elementary scatterplot. Let's kickoff with some x
and y
information vectors and a z
grouping factor that we'll use later:
set.seed(100) z <- sample(i:4, 100, True) x <- rnorm(100) y <- rnorm(100)
Let'southward draw the bones scatterplot:
plot(ten, y, pch = 15)
By default, the points in this plot are black. But we can change that color by specifying a col
argument and a character string containing a color. For example, we could brand the points red:
plot(x, y, pch = xv, col = "reddish")
or bluish:
plot(x, y, pch = 15, col = "bluish")
R comes with hundreds of colors, which we can see using the colors()
office. Allow's meet the starting time 25 colors in this:
colors()[one:25]
## [1] "white" "aliceblue" "antiquewhite" "antiquewhite1" ## [5] "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine" ## [9] "aquamarine1" "aquamarine2" "aquamarine3" "aquamarine4" ## [thirteen] "azure" "azure1" "azure2" "azure3" ## [17] "azure4" "biscuit" "bisque" "bisque1" ## [21] "bisque2" "bisque3" "bisque4" "black" ## [25] "blanchedalmond"
Y'all tin can specify any of these colors as is.
Color Vector Recycling
An important attribute of R'south use of the col
argument is the notion of vector recyling. R expects the col
argument to have the same length every bit the number of things its plotting (in this case the number of points). So when nosotros specify col='reddish'
, R actually "recycles" the color ruddy for each point, effectively constructing a vector similar c('red','reddish','red',...)
equal to the length of our data. We can take advantage of recycling to specify multiple colors. For example, we tin can specify every other point in our data as existence red and blueish:
plot(x, y, pch = fifteen, col = c("ruddy", "blueish"))
Of course, these colors are not substantively meaningful. Our data are non organized in an alternating fashion. We did, however, have a grouping gene z
that takes four levels. Nosotros tin imagine that these are 4 substantively important groups in our information that we would like to highlight with different colors. To practise that, we could specify a vector of four colors and index it using our z
vector:
plot(x, y, pch = 15, col = c("red", "blueish", "light-green", "orange")[z])
Now, the four groups each accept their ain color in the resulting plot. Another strategy is to apply the pch
("indicate character") statement to place groups, which we tin can practise using the same logic:
plot(x, y, pch = c(15, xvi, 17, xviii)[z])
Simply I think colors look ameliorate here than different shapes. Of class, sometimes we have to print in grayscale or monochrome, and then finding the all-time combination of shapes and colors may take a chip of piece of work.
Color generation functions
In improver to the named colors, R tin too generate any other color pattern in the rainbow using one of several functions. For example, the rgb
function can generate a colour based on levels of Red, Greenish, and Bluish (thus the rgb
name). For case, the color ruby is simply:
rgb(i, 0, 0)
## [i] "#FF0000"
The event is the colour red expressed in hexidecimal format. Ii other functions - hsv
and hcl
- allow you specify colors in other ways, but rgb
is the easiest, in office, considering hexidecimal format is widely used in web publishing so there are many tools online for figuring out how to create the colour you desire every bit a combination of red, green, and blue. We tin meet that specifying col='red'
or col=rgb(1,0,0)
produce the same graphical result:
plot(x, y, pch = 15, col = "red")
plot(x, y, pch = xv, col = rgb(1, 0, 0))
Just rgb
(and the other color-generation functions) are also "vectorized", meaning that we can supply them with a vector of numbers in order to obtain different shades. For example, to get 4 shades of red, we tin type:
rgb((1:4)/4, 0, 0)
## [ane] "#400000" "#800000" "#BF0000" "#FF0000"
If nosotros index this with z
(equally we did above), we get a plot where are unlike groups are represented by dissimilar shades of red:
plot(x, y, pch = 15, col = rgb((ane:4)/iv, 0, 0)[z])
When nosotros have to impress in grayscale, R besides supplies a office for edifice shades of grey, which is called - unsurprisingly - gray
. The gray
office takes a number between 0 and 1 that specifies a shade of gray between blackness (0) and white (ane):
gray(0.5)
## [1] "#808080"
The response is, again, a hexidecimal color representation. Like rgb
, gray
is vectorized and we can use it to color our plot:
grayness((i:four)/half dozen)
## [1] "#2B2B2B" "#555555" "#808080" "#AAAAAA"
plot(10, y, pch = 15, col = greyness((1:4)/6)[z])
Merely R doesn't restrict us to ane color palette - just 1 colour or just grayscale. Nosotros tin can also produce "rainbows" of color. For case, we could use the rainbow
role to become a rainbow of four dissimilar colors and utilize it on our plot.
plot(x, y, pch = 15, col = rainbow(iv)[z])
rainbow
takes additional arguments, such as start
and end
that specify where on the rainbow (as measured from 0 to i) the colors should come from. So, specifying low values for start
and terminate
will make a red/yellow-ish plot, middling values will produce a greenish/blue-ish plot, and loftier values will prdocue a blue/purple-ish plot:
plot(x, y, pch = fifteen, col = rainbow(4, start = 0, end = 0.25)[z])
plot(x, y, pch = 15, col = rainbow(4, start = 0.35, cease = 0.6)[z])
plot(10, y, pch = 15, col = rainbow(4, get-go = 0.seven, end = 0.nine)[z])
Colour as data ##
Above we've used colour to convey groups inside the data. But nosotros tin can besides utilize colour to convey a third variable on our two-dimensional plot. For instance, we can imagine that we have some effect val
to which ten
and y
each contribute. We want to come across the level of val
as information technology is affected by both x
and y
. Let'south beginning by creating the val
vector as a office of ten
and y
so use it as a color value:
val <- 10 + y
And then let'south rescale val
to be between 0 and ane to make it easier to use in our color functions:
valcol <- (val + abs(min(val)))/max(val + abs(min(val)))
Now we tin can use the valcol
vector to color our plot using gray
:
plot(x, y, pch = 15, col = greyness(valcol))
Nosotros could also employ rgb
to create a spectrum of blues:
plot(x, y, pch = 15, col = rgb(0, 0, valcol))
There are endless other options, merely this conveys the basic principles of plot coloring which rely on named colors or a color generation function, and the general R principles of recycling and vectorization.
How To Use 2 Different Colors In Plot In R,
Source: https://thomasleeper.com/Rcourse/Tutorials/plotcolors.html
Posted by: clementebutcheall.blogspot.com
0 Response to "How To Use 2 Different Colors In Plot In R"
Post a Comment