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 = rno
Something like the following might work. First I set up the colour scale:
plot_data <- tidyr::gather(df, key = 'Variable', value = 'Component', -Index)
vars <- levels(plot_data$Variable)
colours <- character(length(vars))
colours[vars=="Heat"] <- "red"
colours[vars=="Cool"] <- "blue"
colours[vars=="Other"] <- "green"
other_colours <- c("orange", "purple", "brown", "gold")
others <- !(vars %in% c("Heat", "Cool", "Other"))
colours[others] <- other_colours[1:sum(others)]
The idea is to manually assign your desired colours first, and then assign colours from some list to the other elements. If you need more colours for other_colours
, you can get a complete list of named colours using colours()
.
Then the plot is produced by:
ggplot(plot_data, aes(Index, Component, colour = Variable)) +
geom_line() +
scale_colour_manual(values = colours)
I don't think that it is possible to use scale_colour_manual
and still let ggplot
pick some colours automatically.