Let's say I have the following data set:
set.seed(1)
df <- data.frame(
Index = 1:10,
Heat = rnorm(10),
Cool = rnorm(10),
Other = rnorm(10),
a = rnorm(10),
b = rnorm(10)
)
Now I want to make a line graph of each of the columns against Index. I do this the following way:
df.plot <- ggplot(
data = tidyr::gather(df, key = 'Variable', value = 'Component', -Index),
aes(x = Index, y = Component, color = Variable)
) +
geom_line()
but now I want to change it so that the variables Heat, Cool, and Other are red, blue, and green respectively. So I tried something like:
set.colors <- c(Heat = 'red', Cool = 'blue', Other = 'green')
df.plot + scale_color_manual(values = set.colors)
The problem here is that the set.colors variable doesn't have enough colors (a and b aren't represented) but I just want ggplot to automatically assign colors to both of these variables because in my actual code, there's no way of telling how many of these columns there will be. So basically I want ggplot to do it's normal color assignment and then search for any variables that are names Heat, Cool, or Other (there's no guarantee that any or all of these three will be present) and then change their colors to red, blue, and green respectively without changing the colors of any other variable.