I'm working with several dataframes that have a few categorical values and creating charts from them.
For example the data may look like this. Notice that "Marcus" is not in friends_2
friends <- data.frame(
name = factor(c("Nicole", "Sara", "Marcus", "Derek", "Conor", "Anna")),
number = c(.4, .8, 1, .1, .2, .2)
)
friends_2 <- data.frame(
name = factor(c("Nicole", "Sara", "Derek", "Conor", "Anna")),
score = c(50, 10, 20, 90, 15)
)
Here are some code for plots.
I have a color palette that is intended to match the names but because "Marcus" isn't on friends_2, the colors don't match up anymore.
cbp1 <- c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E", "#E6AB02")
ggplot(friends, aes(x=name, y=number)) +
scale_fill_manual(values = cbp1) +
geom_bar(
stat = "identity",
width = 0.5,
aes(fill = name)
)
ggplot(friends_2, aes(x=name, y=score)) +
scale_fill_manual(values = cbp1) +
geom_bar(
stat = "identity",
width = 0.5,
aes(fill = name)
)
What is the best way to make it so the colors are aligned with my names? Thank you!

