G-Bruce
G-Bruce

Reputation: 83

R and GGPLO2: Factor variable to drive Color in Plot

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

Upvotes: 1

Views: 196

Answers (1)

bergant
bergant

Reputation: 7232

See scale_color_identity. For example:

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

Upvotes: 2

Related Questions