Skip to content
Prev 906 / 29559 Next

Best way to plot cross sections of discrete-valued grids

Ah, while typing this email I see Barry's contribution --
Baz, it's time that you take a closer look at what's already
in sp; we're duplicating work.

Here's my reply:

Scott, you can. Consider the following example with
random numbers (1:10) on a 3d 100x100x100 grid.

library(sp)
# create 3D grid
xyz = expand.grid(x = 1:100, y = 1:100, z = 1:100)
d = data.frame(xyz, v = sample(1:10, 1e6, replace = T))
gridded(d) = ~x+y+z
class(d)
summary(d)
# first point on line:
p1 = c(5,5,5)
# second point on line:
p2 = c(95,95,64)
rbind(p1,p2)
pts = sample.Line(rbind(p1,p2), 1000, "regular")
# select grid elements for each of 1000 points:
plot(d$v[overlay(d, pts)], type = 'l')


It "misuses" sample.Line, which was written as a spsample method
for Line objects; too bad that Line objects in sp are limited to exist
in two dimensions. Anyway, this seems to work (although I
obviously didn't just verify that the result was correct!!)
Waichler, Scott R wrote:
See above.
library(lattice)
xyplot(z~1:1000, data.frame(z = d$v[overlay(d, pts)]), type = 'l')
Am I correct that the lines you mention are shown in ghostview/ghostscript?
If that is the case, they result from the gs/gv anti-aliasing 
mechanism--they are
not part of the postscript. Printing to paper will not show it.

If file size is a problem, try pdf and wrap the pdf e.g. using pdflatex, 
or directly
print to bitmap formats such as png.

HTH,
--
Edzer