I have a data frame that looks like this:
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")
Briefly, my data are x,y positions of a specific object (A). I want to subset my data for a specific time (T) in a specific position (X, Y) on three concentric rings. I have read that I could do it on a polygon using the inpip
function of the package splancs
but circles are not polygon. :( Subsetting using time would be easy but for area I just cannot figure it out.
I also tried to subset my data by coordinates using the subset
function but the coordinates also end up as a polygon and not a circle. The only thing I was able to do is to draw concentric circles using:
plot(0,0,type = "n", xlim = c(0,957), ylim = c(0,765))
my.shape=draw.circle (455,351,seq(112,336, 112))
So any help would be great.
I am assuming that when you are drawing your circles that you intend them to mark off distances from the center point in the x and y coordinate space. If that is the case, then the following code will assign to each row of your data frame a group identifier indicating which concentric ring it belongs to.
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, ]
If this is NOT the case ... if you intend for your circles to look like circles even when the x- and y-axes are not scaled the same, which is what draw.circle()
will do if add to a plot that is not equally scaled, then this solution will not do the trick.