我知道如何使用R的默认绘图功能进行绘图.我如何在ggplot2中执行与以下R代码相同的操作?
double <- function(x){ return(x^2) } triple <- function(x){ return(x^3) } xs <- seq(-3,3,0.01) dou <- double(xs) tri <- triple(xs) plot(rep(xs, 2), c(dou, tri), typ="n") lines(xs, dou, col="red") lines(xs, tri, col="green")
使用前无需在绘图前应用这些功能ggplot2
.您可以告诉ggplot2
您使用您的功能.
library(ggplot2) ggplot(as.data.frame(xs), aes(xs)) + stat_function(fun = double, colour = "red") + stat_function(fun = triple, colour = "green")