Friday, May 6, 2011

Conditional plots in R

This link says it all.

 Al - 19 Jan 2007 15:33 GMT
Hi!
Given a 1000x2 matrix x, i.e. 1000 2d-points. How can I directly (!)
plot/visualize only those which fulfill a certain condition, say
x[,1]>0 & x[,2]>0?
Thanks,
Al
Marc Schwartz - 19 Jan 2007 21:24 GMT
> Hi!
> Given a 1000x2 matrix x, i.e. 1000 2d-points. How can I directly (!)
> plot/visualize only those which fulfill a certain condition, say
> x[,1]>0 & x[,2]>0?
> Thanks,
> Al

First, R specific programming queries are best posted to the r-help
list. More information at:

http://www.r-project.org/

under the Mailing Lists link.

Second, the easiest way to do this is to subset the data while using
plot.formula(), which is a plot method that will be dispatched if the
first argument to plot() is a formula, such as 'y ~ x'. See ?plot and
?plot.formula for more information.

As an example, let's create a data frame with 2 columns and 1000 rows,
each column comprised of 1000 random normal deviates:

DF <- data.frame(x = rnorm(1000), y = rnorm(1000))
> str(DF)
'data.frame': 1000 obs. of 2 variables:
$ x: num -0.0265 0.1554 -0.1050 -0.9697 -0.3430 ...
$ y: num 1.386 -1.356 -1.170 0.426 0.204 ...

Now, let's plot the data meeting the criteria you indicated above:

plot(y ~ x, data = DF, subset = (x > 0) & (y > 0))

This says to generate a scatter plot, using 'DF' as the input data set,
and specifying the logical subset where x and y are both > 0. Modify
the subset argument as you require.

Note that in this usage, the 'data' argument must be a data frame. So
you could coerce your matrix (if needed) by using:

DF <- as.data.frame(matrix)

HTH,

Marc Schwartz

And this is the way (ex.) to get the length of the array that satisfies the conditions.
sum((x > 0) & (y > 0), na.rm=TRUE)

No comments:

Post a Comment