How to plot polar coordinates in R?
Asked Answered
T

4

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.

Turtleneck answered 22/3, 2015 at 10:56 Comment(2)
See: docs.ggplot2.org/current/coord_polar.htmlCartridge
The tag for "parametric equations" was the closest I could find for the mathematical concept that this illustrates.Weakkneed
W
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

Weakkneed answered 22/3, 2015 at 19:17 Comment(1)
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_polarWeakkneed
S
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")
Safe answered 23/11, 2016 at 12:3 Comment(0)
G
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.

Gelasias answered 16/1, 2017 at 15:16 Comment(0)
A
0

You could do this with complex numbers:

t <- seq(0,10, len=1000)
plot(complex(modulus =sqrt(t), argument = 2*pi*t), type="l")

enter image description here

Andrel answered 27/2 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.