0

我有一个看起来像这样的数据框:

structure(list(A = c(10, 10, 10, 10, 10, 10), T = c(0.1, 0.2, 
0.3, 0.4, 0.5, 0.6), X = c(673.05, 672.3, 672.3, 672.3, 667.82, 
667.82), Y = c(203.93, 203.93, 203.93, 203.93, 209.16, 209.16
), V = c(14.79, 14.94, 0, 12.677, 14.94, 14.94)), .Names = c("A", 
"T", "X", "Y", "V"), row.names = c(NA, 6L), class = "data.frame")

简而言之,我的数据是特定对象 (A) 的 x,y 位置。我想在三个同心环上的特定位置 (X, Y) 对特定时间 (T) 的数据进行子集化。我读过我可以使用inpip包的功能在多边形上做到这一点,splancs但圆圈不是多边形。:( 使用时间进行子集很容易,但对于我无法弄清楚的区域。

我还尝试使用该subset函数通过坐标对我的数据进行子集化,但坐标也最终成为多边形而不是圆形。我唯一能做的就是使用以下方法绘制同心圆:

plot(0,0,type = "n", xlim = c(0,957), ylim = c(0,765))
my.shape=draw.circle (455,351,seq(112,336, 112))

所以任何帮助都会很棒。

4

1 回答 1

1

我假设当您绘制圆圈时,您希望它们在 x 和 y 坐标空间中标出距中心点的距离。如果是这种情况,那么以下代码将为数据框的每一行分配一个组标识符,指示它属于哪个同心环。

library(plotrix)
library(MASS)

# create fake data
df <- data.frame(X=runif(1000, 0, 1000), Y=runif(1000, 0, 600))

# define the center and radii of the circles
center <- c(455, 351)
radii <- seq(112, 336, 112)

# calculate the distance to the center for each row of object df
distcenter <- apply(df[, c("X", "Y")], 1, function(rowi) dist(rbind(rowi, center)))
# assign each row of object df to a group based on distcenter
group <- cut(distcenter, c(-1, radii, 2*max(radii)), labels=FALSE)

# to ensure that circles are drawn in x,y coordinate space, you need to use the eqscplot() function from package MASS
eqscplot(0, 0, type="n", xlim=range(df$X, center[1]+c(-1, 1)*max(radii)), ylim=range(df$Y, center[2]+c(-1, 1)*max(radii)))
draw.circle(center[1], center[2], radii)
points(df$X, df$Y, col=group)

# to subset all of the rows of a given group, you can use something like this
df[group==2, ]

如果不是这样...... _ draw.circle()_那么这个解决方案将无法解决问题。

于 2013-11-07T13:25:10.120 回答