![]() ![]() Now, the four groups each have their own color in the resulting plot. To do that, we could specify a vector of four colors and index it using our z vector: plot(x, y, pch = 15, col = c("red", "blue", "green", "orange")) We can imagine that these are four substantively important groups in our data that we would like to highlight with different colors. We did, however, have a grouping factor z that takes four levels. Our data are not organized in an alternating fashion. Of course, these colors are not substantively meaningful. For example, we can specify every other point in our data as being red and blue: plot(x, y, pch = 15, col = c("red", "blue")) We can take advantage of recycling to specify multiple colors. So when we specify col='red', R actually “recycles” the color red for each point, effectively constructing a vector like c('red','red','red'.) equal to the length of our data. R expects the col argument to have the same length as the number of things its plotting (in this case the number of points). Color Vector RecyclingĪn important aspect of R's use of the col argument is the notion of vector recyling. You can specify any of these colors as is. # "aquamarine1" "aquamarine2" "aquamarine3" "aquamarine4" # "antiquewhite2" "antiquewhite3" "antiquewhite4" "aquamarine" ![]() # "white" "aliceblue" "antiquewhite" "antiquewhite1" Let's see the first 25 colors in this: colors() R comes with hundreds of colors, which we can see using the colors() function. Or blue: plot(x, y, pch = 15, col = "blue") For example, we could make the points red: plot(x, y, pch = 15, col = "red") But we can change that color by specifying a col argument and a character string containing a color. Let's draw the basic scatterplot: plot(x, y, pch = 15)īy default, the points in this plot are black. Let's start with some x and y data vectors and a z grouping factor that we'll use later: set.seed(100) ![]() To start, we need to have a baseline graph. This tutorial looks at some of these functions. But R provides many functions for carefully controlling the colors that are used in plots. By default, R graphs tend to be black-and-white and, in fact, rather unattractive. But one of the biggest contributors to the “wow” factors that often accompanies R graphics is the careful use of color. The difference between a simple graph and a visually stunning graph is of course a matter of many features. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |