在涉及指定因子级别时更改ggplot2的图例标签的顺序,如this example中所示:
diamonds$cut <- factor(diamonds$cut, levels = rev(levels(diamonds$cut)))
我正在尝试更改使用以下代码生成的此绘图中stat_function
图层的图例标签的顺序:
myfun1 <- function(x) -.167*x + 0.032*x^2 -0.002*x^3 + .694 # low
myfun2 <- function(x) -.167*x + 0.032*x^2 -0.002*x^3 + .694 + 0.249 # mid vs. low
myfun3 <- function(x) -.167*x + 0.032*x^2 -0.002*x^3 + .694 + 0.492 # high vs. low
df <- data.frame(x = c(0, 8), y = c(0, 8)) # make highest first
myplot <- ggplot(data = df, aes(x = x, y = y)) + # maybe need to change x-axis labels
stat_function(fun = myfun1, aes(color = "Lowest")) +
stat_function(fun = myfun2, aes(color = "Average")) +
stat_function(fun = myfun3, aes(color = "Highest")) +
scale_x_continuous(expand = c(0, 0), limits = c(0, 8)) +
scale_y_continuous(expand = c(0, 0), limits = c(0, 1.5)) +
theme_minimal() +
ylab("Proportion of Videos Watched") +
xlab("") +
scale_colour_brewer("Students' Course Grade", type = "qual", palette = 4, direction = -1) +
theme(legend.position = "right",
legend.direction = "vertical") +
theme(text=element_text(size = 16, family= "Times")) +
scale_x_continuous(breaks = 0:8,
labels = paste0("Wave ", 1:9)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
myplot
如何更改这些stat_function
图例标签的顺序,以便对它们进行排序Lowest
,Average
,Highest
?