Ask
Ask

Reputation: 37

manually assign colors to ggplot factor

I have a data frame I am using for a scatter plot and I want to use a factor variant of 8 levels to determine the color of the points. My question is: how do I manually assign colors to those 8 variants instead of having them automatically decided by ggplot?

Upvotes: 1

Views: 2146

Answers (1)

langtang
langtang

Reputation: 24845

You can pass a named vector to the values parameter of the scale_color_manual() function, where the elements of this named vector are the desired colors and the names of this named vector are the values of your factor.

mycolors = c("a" = "red", "b"="blue", "c"="green", <etc>)

ggplot(data, aes(x,y,color=myfactor)) + 
  geom_point() + 
  scale_color_manual(values=mycolors)

Upvotes: 2

Related Questions