1
votes

Summary: In R, I want ggplot to align the color of geom_point based upon a factor variable within the dataframe.

Here is an example of the data:

#CREATE VECTORS
C1<-c(2,2,2,1,1,0,0)
C2<-c(1,1,2,1,0,0,1)
C3<-c("YELLOW", "YELLOW",   "GREEN","RED",  "RED",  "RED",  "RED")

#COMBINE VECTORS - CREATE DATAFRAME
x<- data.frame(cbind(C1,C2,C3))

Use ggplot to create plot:

ggplot(x, aes(C1,C2)) +
  geom_jitter(aes(color=C3)) +
  geom_point(aes(color=C3))

enter image description here

I would like the C3 variable to choose the color in the plot, meaning GREEN=green, RED=red, and YELLOW = yellow

1
You should encode the colour scheme using scale_colour_manual: ... + scale_colour_manual(values = c("GREEN" = "green", "RED" = "red", "YELLOW" = "yellow")) - Russ Hyde
This is probably just a toy example, but in general, don't use color names in your data to map to colors in your plot. Use meaningful labels that correspond to groups in your data, and keep the actual color values as just an implementation detail in your visualization's color scale. Read here for more. - Andrew Milligan
Andrew Milligan: Thank you for your response, your solution worked. You are correct this is a "toy example". I spelled out "YELLOW" = "yellow" just to make sure I got my point across. I see from bergant response is more to what I was looking for in that the color in the plot is completely dependent upon the values in C3: color = C3. Again, thank you for your time. - G-Bruce

1 Answers

2
votes

See scale_color_identity. For example:

ggplot(x, aes(C1, C2, color = C3)) +
  scale_color_identity()+
  geom_jitter() +
  geom_point()