user2498193 user2498193 - 2 months ago 17
R Question

ggplot legend colours in complex plot

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:
plot with correct colours but no 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:
enter image description here

My question is how can I add a colour coded legend to the upper graph without losing control of the line colours ?

Answer

Change the colour= call in aes in the geom_line calls to whatever you want to appear in the legend:

add_lines1 <- lapply(s1, function(i) geom_line(aes_q(y = as.name(i), colour = "Line1")))
add_lines2 <- lapply(s2, function(i) geom_line(aes_q(y = as.name(i), colour = "Line2")))
add_lines3 <- lapply(s3, function(i) geom_line(aes_q(y = as.name(i), colour = "Line3")))
add_lines4 <- lapply(s4, function(i) geom_line(aes_q(y = as.name(i), colour = "Line4")))

then add a scale_color_manual() onto the full ggplot calls to get consistent colors (of your choosing)

p1 <- ggplot(data, aes(x = time)) + scale_color_manual(values=c("orange","yellow","white","green"))
p1 <- p1 + add_lines1 + add_lines2

p2 <- ggplot(data, aes(x = time)) + scale_color_manual(values=c("orange","yellow","white","green"))
p2 <- p2 + add_lines1 + add_lines3 + add_lines4
Comments