Plotting functions on top of datapoints in R

后端 未结 2 923
滥情空心
滥情空心 2020-12-14 23:40

Is there a way of overlaying a mathematical function on top of data using ggplot?

## add ggplot2
library(ggplot2)

# function
eq = function(x){x*x}

# Data           


        
2条回答
  •  情歌与酒
    2020-12-15 00:18

    Given that your question title is "plotting functions in R", here's how to use curve to add a function to a base R plot.

    Create data as before

    eq = function(x){x*x}; x = (1:50); y = eq(x)
    

    Then use plot from base graphics to plot the points followed by curve with the add=TRUE argument, to add the curve.

    plot(x, y,  xlab = "X-axis", ylab = "Y-axis") 
    curve(eq, add=TRUE)
    

提交回复
热议问题