Reputation: 1
I have an issues and have searching and searching. I'm really really new to this R Code. I have 2 different values from my gender array 1=Male and 2=Female.
I can't figure out how to change the scale from gradient to 1 and 2 color, and how I can make different color to the female and male. I close, but can't do the last job :(
ggplot(smokingdata, aes(x=ages, y=consume, col=gender)) +
geom_point() + ylim(0, 80)
Hop there is someone how can help me with that.
Upvotes: 0
Views: 367
Reputation: 1141
The real issue here is that your data is incorrectly specified. gender
is a discrete variable that is stored as a numeric value, so ggplot2
is treating it as a continuous variable. Simply convert this variable to a factor and you will get a discrete color scale.
ggplot(smokingdata, aes(x=ages, y=consume, col=as.factor(gender))) +
geom_point() + ylim(0, 80)
However, this won't generate a very useful legend. Modifying your data frame to represent the variable accurately would yield the best result:
smokingdata$gender <- factor(smokingdata$gender, levels = c(1,2), labels = c("Male", "Female"))
ggplot(smokingdata, aes(x=ages, y=consume, col=gender)) +
geom_point() + ylim(0, 80)
Upvotes: 1
Reputation: 16832
You can give a vector, either named or unnamed, to scale_color_manual
. So if your column for gender is encoded as 1 & 2, you could use scale_color_manual(values = c("1" = "purple", "2" = "orange"))
. Note that if you're using numbers for gender, those need to be turned into characters to work as names in the named vector.
You could also change the gender column to a character vector instead of a numeric one; that way, ggplot
will treat it as a discrete variable, rather than a continuous one.
Upvotes: 0