我需要在R中绘制一个以(0,0)
为中心的圆。然后我想绘制以半径和度数指定的圆圈中的点。有人能指出我正确的方向完成这项任务吗?
答案 0 :(得分:3)
在基础图形中:
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)
个参数
答案 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")
答案 2 :(得分:0)