Reputation: 27
I am trying to make a graph of the periodic function -23.5*cospi/4(x)+23.5
It gives the results I want in Desmos for reference.
However, when I code it as a function and plot it in ggplot, I get a weird-looking periodic function.
x <- 0:32
test <- data.frame(x, y=-23.5*cos(pi/4*x)+23.5)
test2 <- function(x) -23.5*cos(pi/4*x)+23.5
ggplot(data = test, mapping = aes(x,y))+
geom_point()+
stat_smooth(se = FALSE)
The points show as normal, but the stat_smooth gives this lumpy hunk of garbage.
Upvotes: 0
Views: 57
Reputation: 124473
As you want to graph a function I would suggest to use geom_function
:
Note: For smoothness I doubled the default number of interpolation points n
.
x <- 0:32
test <- data.frame(x, y = -23.5 * cos(pi / 4 * x) + 23.5)
test2 <- function(x) -23.5 * cos(pi / 4 * x) + 23.5
library(ggplot2)
ggplot() +
geom_point(data = test, mapping = aes(x, y)) +
geom_function(fun = test2, n = 202)
Upvotes: 2