Cannot Set Colours as Values in Scale_Colour_Discrete [Duplicate]
I'm Creating a Plot That Depicts Several Data Points ("Region") per Individual ("Patient"). I'm Trying to Set the Colours Denoting the Regions to a Manual...
I'm creating a plot that depicts several data points ("region") per individual ("patient"). I'm trying to set the colours denoting the regions to a manual colour blindness-friendly palette:
#data sample
patient <- c("pat1", "pat1", "pat1", "pat1", "pat1", "pat1", "pat2", "pat2", "pat2", "pat2", "pat2", "pat2") %>% as.factor()
region <- c("bg", "cb", "fr", "hc", "sn", "th", "bg", "cb", "fr", "hc", "sn", "th")
abundance <- c(0.00257, 0.00172, 0.0214, 0.00594, 0.00151, 0.00955, 0.00257, 0.00172, 0.0214, 0.00594, 0.00151, 0.00955)
errormax <- c(0.00569, 0.00435, 0.0378, 0.0131, 0.00507, 0.0194, 0.00569, 0.00435, 0.0378, 0.0131, 0.00507, 0.0194)
errormin <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
mydata <- tibble(patient, region, abundance, errormax, errormin)
#palette
cbbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#D55E00", "#CC79A7")
#plot
myplot <- ggplot(mydata, mapping = aes(x = patient, y = abundance, colour = region)) +
geom_point(position = position_dodge(width = 0.6), na.rm = TRUE) +
geom_errorbar(aes(ymin = errormax, ymax = errormin), width = 0.2, position = position_dodge(width = 0.6)) +
ylim(0,0.12)+
xlab("Patient") + ylab("Abundance") +
theme_bw() +
scale_colour_discrete(name="Brain region",
labels = c("basal ganglia", "cerebellum", "frontal cortex", "hippocampus", "midbrain", "thalamus"),
breaks = c("bg", "cb", "fr", "hc", "sn", "th"),
values = cbbPalette)
But the code above gives me this error:
Error in discrete_scale(aesthetics, "hue", hue_pal(h, c, l, h.start, direction), :
unused argument (values = c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#D55E00", "#CC79A7"))
Maybe values is not the correct argument? Should I use something different instead?
You probably need scale_colour_manual.
library(ggplot2)
ggplot(mydata, mapping = aes(x = patient, y = abundance, colour = region)) +
geom_point(position = position_dodge(width = 0.6), na.rm = TRUE) +
geom_errorbar(aes(ymin = errormax, ymax = errormin), width = 0.2,
position = position_dodge(width = 0.6)) +
ylim(0,0.12)+
xlab("Patient") + ylab("Abundance") +
theme_bw() +
scale_colour_manual(name="Brain region",
labels = c("basal ganglia","cerebellum","frontal cortex",
"hippocampus", "midbrain", "thalamus"),
breaks = c("bg", "cb", "fr", "hc", "sn", "th"),
values = cbbPalette)