0

I have an expression

((cos(-0.2 * pi)*(y-1) - (sin(-0.2 * pi)*x))) = 0.2 * (abs( sin((10*pi*(sin(-0.2 * pi) * (y - 1) + cos(-0.2 * pi) * x))^1)))^6

that I want to draw in a ggplot with geom_point plotted points. The function will look similar to this image when plotted: Plotted Function

The problem with the function is probably the overlapping values. I have tried to solve it by using a contour plot as described in this question. This solution produced the following result: Plotted Function with Contour Plot

However, this plot seems imprecise, so I would like to find a better solution for plotting this kind of function in R with ggplot.

Community
  • 1
  • 1
S.B.Wrede
  • 123
  • 1
  • 2
  • 10
  • I am not trying to draw the line with geom_point. I have a set of points that I plot with geom_point _and_ then I also want a line that is based on an expression. The line should not depend on the points. – S.B.Wrede Mar 30 '17 at 10:26

1 Answers1

5

Following the idea of the contour plot, you only have to increase the resolution in x and y. Here you will find a script with your function. The lower the value of resolution here, the higher the precision. Be careful not to use a too high value that would create a very long calculation...
It starts to be good with 0.05:

library(ggplot2)
resolution <- 0.005
xy <- expand.grid(x = seq(0, 1, resolution), y = seq(0, 1, resolution))
z <- apply(xy, 1, function(xy) {
  x <- xy[1]
  y <- xy[2]
  0.2 * (abs( sin((10*pi*(sin(-0.2 * pi) * (y - 1) + cos(-0.2 * pi) * x))^1)))^6 - 
  ((cos(-0.2 * pi)*(y-1) - (sin(-0.2 * pi)*x)))
})

xyz <- data.frame(xy, z)

ggplot() +
geom_contour(data=xyz,
             aes(x=x,y=y,z=z),breaks=0,
             colour="black")
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
  • Thank you, it worked perfectly! I cannot upvote because of my reputation, but i would have done it if I could. – S.B.Wrede Mar 30 '17 at 12:53