Plot function line on scatter plot R -
i have function
eq = function(x){x*2} for line plot on scatter plot in r.
my attempt follows (have not posted data, not runnable, syntacticly equivalent):
plot(per_class$input_kb, per_class$kb, col="red", ylab="peak memory usage (kb)", xlab="input data (kb)", main="peak memory per input size") which give me plot. try
lines(eq,y=null) however, error.
error in as.double(y) : cannot coerce type 'closure' vector of type 'double' is there way plot function on plot in r (ideally without ggplot2)? or have make data frame representing function...seems kinda hacked.
when plotting function, can either use lines or use curve.
the error arising because lines requires vector (or matrix) arguments (type 'double' in error message) , feeding function (type 'closure').
the curve function designed directly plotting functions , may preferable. below example of plotting 2 functions estimate set of points.
# sample data set.seed(1234) x <- 1:20 y <- x^2 + 0.5 * x + 1 + rnorm(20) y.est1 <- function(x) 1.1 * x^2 + 0.55 * x - 1 y.est2 <- function(x) .9 * x^2 + 0.45 * x + 2 # scatter plot plot(x, y) using lines plot first estimate in red.
lines(x, y.est1(x), col="red") using curve plot second estimate in blue.
curve(y.est2, from=min(x), to=max(x), col="blue", add=true) for curve need include add=true argument, otherwise, curve create new plot.
this produces following figure:

Comments
Post a Comment