How to plot polar coordinates in R?

大城市里の小女人 提交于 2019-12-01 03:38:28

问题


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.


回答1:


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()

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()




回答2:


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")



回答3:


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.



来源:https://stackoverflow.com/questions/29193499/how-to-plot-polar-coordinates-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!