Plotting functions on top of datapoints in R -
Is it possible to overlay a mathematical function at the top of the data using ggplot?
## Add gaglot2 library (ggplot2) # function eq = function (x) {x * x} # data x = (1:50) y = eq (x) # plot object P = qplot (x, y, xlab = "x-x", ylab = "y-axis",) # plot equation c = curve (eq) # data and function p + c # combine?
In this case my data is generated using the function, but I understand how to do curve ()
with ggplot.
You probably want:
library ("ggplot2") Eq & lt; - Function (x) {X * x} TMP & lt; - data.frame (x = 1: 50, y = eq (1:50)) Plot object P & lt; - qplot (x, y, data = tmp, xlab = "x-axis", ylab = "y-axis") C & lt; - stat_function (fun = eq) print (p + c)
and if you want to actually use the curve ()
, that is, calculated X and y coordinate:
qplot (x, y, data = as.data.frame (curve (eq)), geom = "line")
< / Div>
Comments
Post a Comment