Thanks, but I'm sorry that I'll have one more question regarding this. I'm just learning ggplot2, so please don't mind.
I'm assuming that in the complete dataset, there'll be repetitions. What do you suggest to resolve this?
library(ggplot2)
test_df = data.frame(x = c(1, 2, 3, 5),
y = c(2, 3, 4, 6),
tissue = c("nerve", "brain", "heart", "nerve"),
color_code = c("#FF0000", "#00FF00", "#0000FF", "#FF0000"))
ggplot(data = test_df,
mapping = aes(x = x,
y = y,
color = color_code)) +
geom_point() +
scale_color_identity(guide = "legend",
labels = c("nerve", "brain", "heart"),
breaks = test_df$color_code) +
labs(color = "Tissue")
#> Error: `breaks` and `labels` must have the same length
Created on 2019-03-16 by the reprex package (v0.2.1)
Actually, I find @aosmith's solution much easier for this situation, with some modification for the alphabetical ordering.
library(ggplot2)
test_df <- data.frame(x = c(1, 2, 3, 5),
y = c(2, 3, 4, 6),
tissue = c("nerve", "brain", "heart", "nerve"),
color_code = c("#FF0000", "#00FF00", "#0000FF", "FF0000"),
stringsAsFactors = FALSE)
ggplot(data = test_df,
mapping = aes(x = x,
y = y,
color = tissue)) +
geom_point() +
scale_color_manual(values = test_df$color_code[order(test_df$tissue)])

Created on 2019-03-16 by the reprex package (v0.2.1)