So this is a follow on from this question: ggplot: aes vs aes_string, or how to programmatically specify column names?
I adapted the solution from this question for my own purposes but I'm having trouble with the legend. Here I have set up an example:
# Set up some example data
colnames <- c(paste0 ( "y", 1:30))
nr <- 5
m1 <- matrix(data = runif(nr * length(colnames)), nrow=nr, ncol=length(colnames))
data <- data.frame(time=1:nr, m1)
names(data) <- c("time", colnames)
# Define groups of columns
s1 <- sample(colnames, 3)
s2 <- sample(colnames[!colnames %in% s1], 3)
s3 <- sample(colnames[!colnames %in% c(s1, s2) ], 3)
s4 <- sample(colnames[!colnames %in% c(s1, s2) ], 3)
# Code to add lines to the graph by column group
add_lines1 <- lapply(s1, function(i) geom_line(aes_q(y = as.name(i)), colour = "red"))
add_lines2 <- lapply(s2, function(i) geom_line(aes_q(y = as.name(i)), colour = "blue"))
add_lines3 <- lapply(s3, function(i) geom_line(aes_q(y = as.name(i)), colour = "orange"))
add_lines4 <- lapply(s4, function(i) geom_line(aes_q(y = as.name(i)), colour = "purple"))
# Draw plots with different column groups
p1 <- ggplot(data, aes(x = time))
p1 <- p1 + add_lines1 + add_lines2
p2 <- ggplot(data, aes(x = time))
p2 <- p2 + add_lines1 + add_lines3 + add_lines4
grid.arrange( p1, p2)
So as you can see - this produces a graph with the colours I want but not legend:
So I can modify the addlines
to try to remedy this as follows (the difference is in the brackets after as.name(i)
):
add_lines1 <- lapply(s1, function(i) geom_line(aes_q(y = as.name(i), colour = "red")))
add_lines2 <- lapply(s2, function(i) geom_line(aes_q(y = as.name(i), colour = "blue")))
add_lines3 <- lapply(s3, function(i) geom_line(aes_q(y = as.name(i), colour = "orange")))
add_lines4 <- lapply(s4, function(i) geom_line(aes_q(y = as.name(i), colour = "purple")))
This will give me a graph with the legend - but now I have lost control of the colours. Worse the colours are not consistent across the graphs:
My question is how can I add a colour coded legend to the upper graph without losing control of the line colours ?