How can make the witch of Agnesi graph in R

时间:2017-08-13 13:47:33

标签: r graph

I am new to R programming and I am trying to create the witch of Agnesi which is should be like this:

enter image description here

This is my code:

dat<- data.frame(t=seq(0.07*2*pi, 0.43*2*pi, by=0.1) )
 m <-  function(t) 2*(1/(tan(t)))
 n <- function(t) (2*sin(t)^2)
ggplot(dat, aes(x=t)) + stat_function(fun=m)+stat_function(fun=n)

Unfortunately, my graph looks like this:

enter image description here

I really appreciate any advice

1 个答案:

答案 0 :(得分:0)

ggplot will only plot y against x, so this sort of parametrised curve in t will not work - you are just getting the two x and y plots in terms of t. As an alternative, you could do the following...

dat<- data.frame(t=seq(0.07*2*pi, 0.43*2*pi, by=0.1))
m <-  function(t) 2*(1/(tan(t)))
n <- function(t) (2*sin(t)^2)
dat$x <- m(dat$t)
dat$y <- n(dat$t)
ggplot(dat, aes(x=x,y=y)) + geom_line() + ylim(0,NA)

enter image description here

If you want the generating circle as well, you could add this to the ggplot term...

+ coord_fixed() #to keep the x and y dimensions equal
+ annotate("path", x=cos(seq(0,2*pi,length.out=100)),
                   y=1+sin(seq(0,2*pi,length.out=100)),
                   color="blue")