I have data set that has different colors in a variable, my_color
.
library(tidyverse)
set.seed(1234)
dat <- tibble(x = 1:5,
y = 1:5,
my_color = sample(colors(), 5))
dat
#> # A tibble: 5 × 3
#> x y my_color
#> <int> <int> <chr>
#> 1 1 1 grey23
#> 2 2 2 darksalmon
#> 3 3 3 tan3
#> 4 4 4 violetred4
#> 5 5 5 lightblue1
I want to plot the points, and each point to be colored according to the color in my_color
. I can a color aesthetic using aes()
, but the colors are auto-assigned by ggplot2. This is normally a good thing, but in this instance I have specific values I want.
ggplot(dat, aes(x = x, y = y, color = my_color)) +
geom_point(size = 3)
My only work around currently is to manually assign each color to itself, e.g.,
ggplot(dat, aes(x = x, y = y, color = my_color)) +
geom_point(size = 3) +
scale_color_manual(values = c("darksalmon" = "darksalmon",
"grey23" = "grey23",
"lightblue1" = "lightblue1",
"tan3" = "tan3",
"violetred4" = "violetred4"))
This is not super feasible for a real data set with many more than 5 rows. Is there a way to parse aesthetic values from a given variable directly?
Created on 2022-07-26 by the reprex package (v2.0.1)