I am trying to create a legend in ggplot. If I use different variables from the same file, I add colour = "xx"
in aes and it works. but what about if it is the same variable but different datasets?
In the example below, I plot Value ~ Year from two different datasets. How can I create a legend that says df1 with a red line, and df2 with a blue line?
A <- c(2001, 2002, 2003, 2004, 2005)
B <- c(3, 5, 2, 7, 5)
C <- c(2, 7, 4, 3, 5)
df1 <- data.frame(A, B)
df2 <- data.frame(A, C)
colnames(df1) <- c("Year","Value")
colnames(df2) <- c("Year","Value")
(test <- ggplot(df1, aes(Value, Year)) + geom_path(size = 1, colour='red') +
geom_path(data=df2, colour='blue') + ylab("Year")+ scale_x_continuous(position = "top") + scale_y_reverse(expand = c(0, 0)))