Rasmus Larsen
Rasmus Larsen

Reputation: 6117

ggplot2 plot of trigonometric function looks jagged

The curves of trigonometric function plotted with ggplot2 looks jagged.

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

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

Gives this jagged plot:

enter image description here

But I was expecting a more smooth plot like this:

enter image description here

Upvotes: 2

Views: 495

Answers (1)

Sandipan Dey
Sandipan Dey

Reputation: 23099

How about this?

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

Upvotes: 5

Related Questions