用于ggplot的曲线()的等价 [英] Equivalent of curve() for ggplot

查看:76
本文介绍了用于ggplot的曲线()的等价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个等价的方法用于绘制基本图形中使用的 ggplot curve()命令的函数?我猜想替代方法是创建一个函数值向量并绘制一条连接线,但我希望更简单些。

Is there an equivalent method for plotting functions using ggplot to the curve() command employed in base graphics? I guess that the alternative would be to just create a vector of values of the function and plot a connected line, but I was hoping for something a bit more simple.

谢谢!

推荐答案

您可以使用 stat_function 添加曲线:

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

也可以使用 qplot ,但不清楚这是否简单:

It is also possible to use qplot, but it's not clear if this is simplier:

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

如果曲线函数更加复杂,那么使用lambda函数。例如,

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))

您可以在
找到其他示例 http://kohske.wordpress.com/2010/12/25/draw-function-without-data-in-ggplot2/

这篇关于用于ggplot的曲线()的等价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆