Reputation: 1187
MWE:
library(ggplot2)
library(dplyr)
df <- data.frame(GROUP = factor(c("AL", "AS", "B", "HI", "I", "P", "T", "W", "E", "NE", "EL", "EP", "SW", "SOD"), ordered = TRUE),
color.codes = c("#000000",
"#b05cc6",
"#afb045",
"#7777c8",
"#db8f47",
"#4dacd0",
"#cb5242",
"#56b88f",
"#c75a88",
"#5db74e",
"#4f7e3b",
"#ff0000",
"#8d7134",
"#000080"),
Year = rep(paste0(2010:2025, "-", 2011:2026), each=14),
value = runif(224))
df$Year <- factor(df$Year,
levels=sort(unique(df$Year)),
ordered=TRUE)
ggplot(df,
aes(x = Year, y = value, color = GROUP, group = GROUP)) +
geom_point() +
geom_line(linetype = "dotted") + # make all dotted by default
geom_line(data = (df %>% filter(Year <= "2014-2015")),
aes(x = Year, y = value, color = GROUP),
lty = "solid") + # and then make all prior to 2015 solid
scale_color_manual(values = df$color.codes) +
xlab("School Year") +
ylab("Proficiency Rate")
Let's examine the data frame:
> head(df)
GROUP color.codes Year value
1 AL #000000 2010-2011 0.5540173
2 AS #b05cc6 2010-2011 0.0301139
3 B #afb045 2010-2011 0.7469069
4 HI #7777c8 2010-2011 0.4981707
5 I #db8f47 2010-2011 0.9677573
6 P #4dacd0 2010-2011 0.7777855
For example, #b05cc6
, according to Google, is something of a purple shade. But it clearly looks red, despite that it's supposed to be purple.
It also doesn't make any sense that B
has the same color as AL
. How can this be fixed?
Upvotes: 1
Views: 231
Reputation: 94277
Make a palette of group names and colour code mappings:
pal = unique(df[,c("GROUP","color.codes")])
> head(pal)
GROUP color.codes
1 AL #000000
2 AS #b05cc6
3 B #afb045
4 HI #7777c8
5 I #db8f47
6 P #4dacd0
Fixup color codes to be character and not factor, or else they get converted to numeric and looked up in the palette:
> pal$color.codes=as.character(pal$color.codes)
Now create a named vector from the palette columns:
> pval = pal$color.codes
> names(pval) = pal$GROUP
> pval
AL AS B HI I P T W
"#000000" "#b05cc6" "#afb045" "#7777c8" "#db8f47" "#4dacd0" "#cb5242" "#56b88f"
E NE EL EP SW SOD
"#c75a88" "#5db74e" "#4f7e3b" "#ff0000" "#8d7134" "#000080"
Then use that in scale_color_manual(values=pval)
.
Now AS looks purply, B looks like a sort of greeny vom colour.
EP looks red. etc.
Upvotes: 4