Reputation: 377
How do I create a scatter-plot in ggplot() with each points coloured manually? The necessary colours are given in my dataframe.
> head(df)
x y col
1 0.72 2757 #2AAE89
2 0.72 2757 #2DFE83
3 0.72 2757 #40FE89
4 0.70 2757 #28FE97
5 0.86 2757 #007C7D
6 0.75 2757 #24FEA1
The colour of the points must be exactly as given in the dataframe
Upvotes: 0
Views: 671
Reputation: 38063
Luckily there is a relatively easy solution by using scale_colour_identity()
, see the following example:
library(ggplot2)
z <- " x y z col
1 0.72 2757 86 #2AAE89
2 0.72 2757 86 #2DFE83
3 0.72 2757 86 #40FE89
4 0.70 2757 82 #28FE97
5 0.86 2757 26 #007C7D
6 0.75 2757 79 #24FEA1"
df <- read.table(text = z, header = T)
ggplot(df, aes(x, y, colour = col)) +
geom_point() +
scale_colour_identity()
EDIT: I made a mistake in loading in the data, but the plotting syntax is still valid.
Upvotes: 3