【问题标题】:How do I plot a circle with points inside it in R?如何在 R 中绘制一个带有点的圆?
【发布时间】:2023-03-31 00:31:01
【问题描述】:

我需要在 R 中以(0,0) 为中心绘制一个圆。然后我想在该圆中绘制以半径和度数指定的点。谁能为我指出这项任务的正确方向?

【问题讨论】:

  • 你的意思是半径和度数吗?

标签: r geometry


【解决方案1】:

在基础图形中:

r <- 3*runif(10)
degs <- 360*runif(10)

# First you want to convert the degrees to radians

theta <- 2*pi*degs/360

# Plot your points by converting to cartesian

plot(r*sin(theta),r*cos(theta),xlim=c(-max(r),max(r)),ylim=c(-max(r),max(r)))

# Add a circle around the points

polygon(max(r)*sin(seq(0,2*pi,length.out=100)),max(r)*cos(seq(0,2*pi,length.out=100)))

请注意,至少有一个点位于圆圈的边界上,因此如果您不希望这样,您可以将 max(r) 状态替换为 1.1*max(r) 之类的内容

【讨论】:

  • 在绘图语句中添加, asp = 1 以使最终图像显示为圆形而不是椭圆形。
【解决方案2】:

要使用 ggplot2 执行此操作,您需要使用 coord_polar,ggplot2 将为您完成所有转换。代码示例:

library(ggplot2)
# I use the builtin dataset 'cars'
# Normal points plot
ggplot(aes(x = speed, y = dist), data = cars) + geom_point() 

# With polar coordinates
ggplot(aes(x = speed, y = dist), data = cars) + geom_point() + 
     coord_polar(theta = "dist")

【讨论】:

  • 谢谢,这看起来很完美,但我不断收到Error in match.arg(theta, c("x", "y")) : 'arg' should be one of “x”, “y”
  • 这个问题是我不能以度数指定位置
  • 如果您想要更多中肯的反馈,请发布可重现的示例。
  • 如果将coord_polar(theta = "dist") 更改为coord_polar(theta = "y"),该示例将起作用
【解决方案3】:

使用极坐标系。(link to wiki)。

然后将transle转入笛卡尔坐标系(link

然后转换为屏幕坐标(例如 0,0 是显示器的中心)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多