ggplot2三角函数图看起来像是锯齿状

时间:2016-09-15 08:20:44

标签: r ggplot2

用ggplot2绘制的三角函数曲线看起来是锯齿状的。

library(ggplot2)
ggplot( data.frame( x = c(-0.2,0.2)), aes( x = x)) + 

stat_function( fun = function(x) cos( pi/x)   , geom = "line")

给出了这个锯齿状的情节:

enter image description here

但我期待这样一个更流畅的情节:

enter image description here

1 个答案:

答案 0 :(得分:5)

这个怎么样?

library(ggplot2)

ggplot(data.frame( x = c(-2.5,2.5)), aes( x = x)) + 
       stat_function(fun = function(x) ifelse(x!=0, cos(pi/x), 0), 
       geom = "line", n=5000, col='blue')

enter image description here