I mocked up the following code to provide context. The functions, sequence intervals, names, and colors are all arbitrary.
library(ggplot2)
function1 <- function(input) {
input * 3
}
function2 <- function(input) {
2 * input + 1
}
function3 <- function(input) {
input + 4
}
x1 <- seq(1, 10, 0.1)
x2 <- seq(1, 10, 0.2)
x3 <- seq(1, 10, 0.5)
y1 <- sapply(x1, function1)
y2 <- sapply(x2, function2)
y3 <- sapply(x3, function3)
data1 <- data.frame(x1, y1)
data2 <- data.frame(x2, y2)
data3 <- data.frame(x3, y3)
ggplot() +
geom_point(data = data1, aes(x1, y1, color = "B")) +
geom_point(data = data2, aes(x2, y2, color = "C")) +
geom_point(data = data3, aes(x3, y3, color = "A")) +
scale_color_manual(name = "Functions",
values = c("B" = "Green", "C" = "Red",
"A" = "Blue")) +
xlab("X") +
ylab("Y")
Here is a screenshot of the resulting plot:
There are a few previously answered questions that address similar issues with legend ordering, such as this one, but none seem to deal with multilayer plots. This question addresses ordering of legends for version 0.9.2, but ggplot2 is currently on version 2.2.1. Additionally, it seems to address only ascending or descending orders.
I'd like to know if there's any way to customize the order of the values in the legend. For example, in the legend, is it possible to display it as B, C, A instead of A, B, C?