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.)?
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.
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.
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.
nlevels() function in R Language is used to get the number of levels of a factor.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With