【问题标题】:Identifying and subsetting data points per variable on concentric circles识别和子集同心圆上每个变量的数据点
【发布时间】:2013-11-07 11:40:00
【问题描述】:

我有一个如下所示的数据框:

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) 的数据进行子集化。我读过我可以使用包splancsinpip 函数在多边形上执行此操作,但圆圈不是多边形。 :( 使用时间进行子集很容易,但对于区域我无法弄清楚。

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

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

所以任何帮助都会很棒。

【问题讨论】:

    标签: r dataframe coordinates subset geometry


    【解决方案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, ]
    

    如果情况不是...如果您打算让您的圈子看起来像圈子,即使 x- 和 y -axes 的缩放比例不同,这就是 draw.circle() 如果添加到比例不相等的绘图中会做的事情,那么这个解决方案将无法解决问题。

    【讨论】:

    • 是的,你的假设是正确的,当我在我的数据上尝试它时,这非常有效。谢谢。
    猜你喜欢
    • 2022-08-03
    • 2017-06-29
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多