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
Mixing your own colors in with a default color palette is a breathtakingly bad idea. Nevertheless, here is one way to do it - similar to the other answer but perhaps a bit more general, and uses ggplot's default color palette for everything else as you asked.
library(ggplot2)
library(reshape2)
gg.df <- melt(df, id="Index", value.name="Component")
ggp <- ggplot(gg.df, aes(x = Index, y = Component, color = variable)) +
geom_line()
lvls <- levels(gg.df$variable)
cols <- setNames(hcl(h=seq(15, 375, length=length(lvls)+1), l=65, c=100),lvls)
cols[c("Heat","Cool","Other")] <- c("#FF0000","#0000FF","#00FF00")
ggp + scale_color_manual(values=cols)
Edit: Just realized that I never said why this is a bad idea. This post gets into it a bit, and has a few really good references. The main point is that the default colors are chosen for a very good reason, not just to make the plot "look pretty". So you really shouldn't mess with them unless there's an overwhelming need.
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.