Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign colors to each level of factors in R figures

Tags:

r

colors

ggplot2

I have a data set with one column which is a factor variable with 4 levels: 1, 2, 3, and 4. I used this codes to assign each level with a different color:

colorset = c('red', 'blue', 'green', 'black')
ggplot(...) + geom_density() + scale_fill_manual(values=colorset)

The code works fine if the data set contains all 4 levels. But sometime the data set is missing one level, for example, missing 2. Then the red is still for level 1, but now blue is assigned to level 3, and green for level 4, while black is never used.

How do I change the code to make sure that, no matter what data set I use, the color assignment remain the same (like blue always for level 2, green always for level 3 etc.)?

like image 241
user3768495 Avatar asked Jan 08 '16 21:01

user3768495


People also ask

How do I assign a color to a variable in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.

How do you set factor levels in R?

One way to change the level order is to use factor() on the factor and specify the order directly. In this example, the function ordered() could be used instead of factor() . Another way to change the order is to use relevel() to make a particular level first in the list.

What is a factor in R and what are levels of a factor?

Factors are the data objects which are used to categorize the data and store it as levels. They can store both strings and integers. They are useful in the columns which have a limited number of unique values. Like "Male, "Female" and True, False etc. They are useful in data analysis for statistical modeling.

How do you know how many levels a factor has in R?

nlevels() function in R Language is used to get the number of levels of a factor.


1 Answers

This is how you do it:

n <- 1000
A <- data.frame(id='A',x=rnorm(n, 5, 2))
B <- data.frame(id='B',x=rexp(n, 1/4))
C <- data.frame(id='C',x=rexp(n, 1/8))
D <- data.frame(id='D',x=rexp(n, 1/16))
df <- rbind(A,B,C,D)

colorset = c('B'='red','A'='green','D'='black','C'='blue'  )

ggplot(df, aes(x)) +
  geom_density(aes(fill = id), alpha = .4, adjust = 2) +
  scale_fill_manual(values=colorset) +
  scale_x_continuous( limits =c(0,40))

enter image description here

like image 105
Mike Wise Avatar answered Sep 28 '22 05:09

Mike Wise