【发布时间】:2023-03-31 00:31:01
【问题描述】:
我需要在 R 中以(0,0) 为中心绘制一个圆。然后我想在该圆中绘制以半径和度数指定的点。谁能为我指出这项任务的正确方向?
【问题讨论】:
-
你的意思是半径和度数吗?
我需要在 R 中以(0,0) 为中心绘制一个圆。然后我想在该圆中绘制以半径和度数指定的点。谁能为我指出这项任务的正确方向?
【问题讨论】:
在基础图形中:
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 以使最终图像显示为圆形而不是椭圆形。
要使用 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"),该示例将起作用
【讨论】: