Plot a function with ggplot, equivalent of curve()

前端 未结 2 1616
一整个雨季
一整个雨季 2020-11-28 05:01

Is there an equivalent method for plotting functions using ggplot to the curve() command employed in base graphics? I guess that the alternative wo

2条回答
  •  感情败类
    2020-11-28 05:23

    You can add a curve using the stat_function:

    ggplot(data.frame(x=c(0, 10)), aes(x)) + stat_function(fun=sin)
    

    If your curve function is more complicated, then use a lambda function. For example,

    ggplot(data.frame(x=c(0, 10)), aes(x)) + 
      stat_function(fun=function(x) sin(x) + log(x))
    

    you can find other examples at http://kohske.wordpress.com/2010/12/25/draw-function-without-data-in-ggplot2/


    In earlier versions, you could use qplot, as below, but this is now deprecated.

    qplot(c(0,2), fun=sin, stat="function", geom="line")
    

提交回复
热议问题