7

Suppose that (x(t),y(t)) has polar coordinates(√t,2πt). Plot (x(t),y(t)) for t∈[0,10].

There is no proper function in R to plot with polar coordinates. I tried normal plot by giving, x=√t & y=2πt. But resultant graph was not as expected.

I got this question from "Introduction to Scientific Programming  and Simulation using r"and the book is telling the plot should be spiral.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Athul
  • 83
  • 1
  • 1
  • 4

3 Answers3

10

Make a sequence:

t <- seq(0,10, len=100)  # the parametric index
# Then convert ( sqrt(t), 2*pi*t ) to rectilinear coordinates
x = sqrt(t)* cos(2*pi*t) 
y = sqrt(t)* sin(2*pi*t)
png("plot1.png");plot(x,y);dev.off()

enter image description here

That doesn't display the sequential character, so add lines to connect adjacent points in the sequence:

png("plot2.png");plot(x,y, type="b");dev.off()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • The first item is typically labeled "r" in polar coordinates, i.e. the radius, and the second item is typically labeled "theta", the angle in radians from the horizontal vector going to the right. Notice that the distance between successive loops of the spiral is decreasing. That's due to the sqrt function being applied to the radial dimension. Ir you just plotted (t, 2*pi*t) the space would stay constant. (more like a spiderweb.) The book is not actually correct about R not having polar plots. The package 'plotrix' has `polar.plot` and package 'ggplot2' has `coord_polar` – IRTFM Mar 23 '15 at 16:52
3

As already mentioned in a previous comment, R can plot using polar coordinates. The package plotrix has a function called polar.plot that does this. Polar coordinates are defined by length and angle. This function can take a sequence of lengths and a sequence of angles to plot with polar coordinates. For example to make one spiral:

library(plotrix)
plt.lns <- seq(1, 100, length=500)
angles <- seq(0, 5*360, length=500)%%360
polar.plot(plt.lns, polar.pos=angles, labels="", rp.type = "polygon")
DorinPopescu
  • 715
  • 6
  • 10
1

An option worth a try, It is Plotly package.

library(plotly)

p <- plot_ly(plotly::mic, r = ~r, t = ~t, color = ~nms, alpha = 0.5, type = "scatter")

layout(p, title = "Mic Patterns", orientation = -90)

Note: If you are using RStudio, the plots are going to be shown in Viewer tab.

Fábio
  • 771
  • 2
  • 14
  • 25