如何在R中绘制一个带有点的圆圈?

时间:2011-12-12 10:43:34

标签: r geometry

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

3 个答案:

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

enter image description here

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

enter image description here

答案 2 :(得分:0)

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

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

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