2016-09-15 165 views
2

用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

+3

增加'N'。參見'?stat_function'。 – Axeman

回答

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

+0

使用零作爲極限,傷害。至少使用'NA'。 (它不會改變情節。) – Roland

+0

當然,我們應該使用NA @Roland –