【问题标题】:How to plot polar coordinates in R?如何在R中绘制极坐标?
【发布时间】:2015-03-22 10:56:05
【问题描述】:

假设 (x(t),y(t)) 具有极坐标 (√t,2πt)。为 t∈[0,10] 绘制 (x(t),y(t))。

R 中没有适当的函数来绘制极坐标。我通过给出 x=√t & y=2πt 来尝试正常绘图。 但结果图并不如预期。

我从“Introduction to Scientific Programming and Simulation using r”中得到了这个问题,这本书告诉我们情节应该是螺旋式的。

【问题讨论】:

标签: r polar-coordinates parametric-equations


【解决方案1】:

制作一个序列:

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

那不显示连续字符,所以添加线连接序列中的相邻点:

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

【讨论】:

  • 第一项通常在极坐标中标记为“r”,即半径,第二项通常标记为“theta”,即从水平向量向右的弧度角。请注意,螺旋的连续循环之间的距离正在减小。这是由于 sqrt 函数应用于径向尺寸。您刚刚绘制 (t, 2*pi*t) 空间将保持不变。 (更像是蜘蛛网。)这本书实际上并不正确,因为 R 没有极坐标图。包'plotrix'有polar.plot,包'ggplot2'有coord_polar
【解决方案2】:

正如前面评论中已经提到的,R 可以使用极坐标进行绘图。包 plotrix 有一个名为 polar.plot 的函数来执行此操作。极坐标由长度和角度定义。此函数可以采用一系列长度和一系列角度来绘制极坐标。例如制作一个螺旋:

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】:

    值得一试的选项,它是Plotly 包。

    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)
    

    注意:如果您使用的是 RStudio,绘图将显示在查看器选项卡中。

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多