[R] A question about R image function

Sarah Goslee sarah.goslee at gmail.com
Wed Oct 5 21:26:48 CEST 2011


Hi,

On Wed, Oct 5, 2011 at 1:44 PM, JeffND <Zuofeng.Shang.5 at nd.edu> wrote:
> Dear folks,
>
> I have a question about the image() function in R. I found the following
> link talking about this
> but the replies didn't help with my situations.
>
> http://r.789695.n4.nabble.com/question-on-image-function-td839275.html#a839276

In fact, that link states exactly what needs to be done, and why.

> To be simple, I will keep using the example in the above link.
>
> Suppose the data are like
>
>         x                y              mcpvalue
> 0.4603578     0.6247629       1.001
> 0.4603715     0.6247788        1.001
> 0.4603852     0.6247948       1.001
> 0.4110561     0.5664841       0.995
>
> So we have four points with coordinates given by the four pairs of (x,y)
> values.
> Each point is associated with a mcpvalue.

> How do we use image() to plot "mcpvalue" as a two-dimensional plot?
> I hope that the points are positioned by the coordinates and the color of
> each point
> in that plot is changing with the value of mcpvalue.

We don't. image() is intended to plot data on a regular grid, and that's not a
regular grid. You need to use kriging or some other form of spatial
interpolation
to fit it to a regular grid before you can use image().

You can however use plot() with the col= argument to plot points at the
actual values of the coordinates, and to color them as desired.


testdata <- structure(list(x = c(0.4603578, 0.4603715, 0.4603852, 0.4110561
), y = c(0.6247629, 0.6247788, 0.6247948, 0.5664841), mcpvalue = c(1.001,
1.001, 1.001, 0.995)), .Names = c("x", "y", "mcpvalue"), class =
"data.frame", row.names = c(NA, -4L))

testdata.colors <- cut(testdata$mcpvalue, c(0, 1, 2))
testdata.colors <- rainbow(length(testdata.colors))[testdata.colors]

with(testdata, plot(x, y, col=testdata.colors))


Which is kind of useless in this case, but may work with your actual
data, which is why I
wrote it out so elaborately.

> Using image() does not work as the coordinates of the points
> are not ascending.

Exactly. So you can't use image().



-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list