2012-04-10 18 views
13

Widzę wiele pytań dotyczących dostosowywania legend, ale nie mogę nawet dostosować legendy. Chciałbym mieć legendę wyjaśniającą, że czarna linia jest kwadratowa, a zielona linia jest sześcienna.ggplot2 dodaj legendę dla kilku funkcji stat_funkcji

library(ggplot2) 

myfun1 <- function(x) x^2 
myfun2 <- function(x) x^3 

myplot <- ggplot(data = data.frame(x = 1:5, y= 1:5), aes(x=x, y=y)) + 
    stat_function(fun = myfun1, color="green") + 
    stat_function(fun = myfun2, color="black") 

Odpowiedz

17

Spróbuj tego:

ggplot(NULL, aes(x=x, colour = g)) + 
    stat_function(data = data.frame(x = 1:5, g = factor(1)), fun = myfun1) + 
    stat_function(data = data.frame(x = 1:5, g = factor(2)), fun = myfun2) + 
    scale_colour_manual(values = c("red", "green"), labels = c("quadratic", "cubic")) 

enter image description here

+0

dziękuję kohske –

Powiązane problemy