Reputation: 89
I have a data frame (see below) and I want to change the colors of the plot in ggplot2 so that all the A's are colored red, B's blue, C's black, and D's yellow.
myDF
label A B C D
lab1 0.69 0.65 0.73 0.71
lab2 0.43 0.41 0.47 0.41
lab3 0.53 0.47 0.57 0.53
lab4 0.55 0.47 0.60 0.55
lab5 0.53 0.47 0.58 0.53
I tried the following:
p <- ggplot(myDF, aes(x=label))
+ geom_point(aes(y=A))
+ geom_point(aes(y=B))
+ geom_point(aes(y=C))
+ geom_point(aes(y=D))
+ scale_colour_manual(values=c("A"="red", "B"= "blue", "C"="black", "D"="yellow"))
But it gives me an error. If I don't add the scale_colour_manual part, it gives the correct plot but all black dots. How can I do this? Thanks in advance!
Upvotes: 2
Views: 10222
Reputation: 33802
Get your data into long format first. And I'd reconsider yellow on white.
library(tidyr)
library(ggplot2)
myDF %>%
gather(key, value, -label) %>%
ggplot(aes(label, value)) + geom_point(aes(color = key)) +
scale_colour_manual(values=c("red", "blue", "black", "yellow"))
Upvotes: 5