[R-sig-Geo] adding points to spplot grid
Nick Matzke
matzke at berkeley.edu
Mon Nov 8 04:13:31 CET 2010
On 11/6/10 3:03 AM, Edzer Pebesma wrote:
>
>
> On 11/06/2010 01:37 AM, Nick Matzke wrote:
>> Hi,
>>
>> I have been working with spplot. I can get continuous
grids to plot
>> with spplot, but what do I do if I want to plot some
points with x,y
>> coordinates on top of the image?
>>
>> Cheers!
>> Nick
>>
>>
> http://r-spatial.sourceforge.net/gallery/
Thanks! This helped although it took some digging. For
others with this question:
=================================
# I start with some (randomly generated for now)
# * TRUE/FALSE values (samp_hits)
# * x & y columns giving sample locations (sample_locs)
# generate the tmpdata for species #1 (samp_hits, column #1)
tmpdata = as.data.frame(cbind(sample_locs[,1],
sample_locs[,2], samp_hits[,1]))
names(tmpdata) = c("x", "y", "sp1")
coordinates(tmpdata) = ~x+y
# set projection to lon (x) & lat (y), i.e. unprojected;
# find projections with Terminal: proj -lp
proj4string(tmpdata) = CRS("lonlat")
hits = tmpdata[tmpdata$sp1 == 1, ]
misses = tmpdata[tmpdata$sp1 == 0, ]
#plot points for kicks
plot(0,0, xlim=minmax_literal(sample_locs[, 1]),
ylim=minmax_literal(sample_locs[, 2]) )
points(hits, col="blue", pch=19)
points(misses, col="blue", pch=1)
# Make variogram
default_vgm = vgm(0.15, "Exp", 80, 0)
gs = gstat(id="sp001", formula=sp1~1, data=tmpdata, beta =
0.1, model=default_vgm)
vg = variogram(gs)
plot(vg)
# Make a tmpgrid
xs=xmin:xmax
ys=ymin:ymax
# use expand.grid to make all possible combinations of inputs
tmpgrid = expand.grid(xs, ys)
names(tmpgrid) = c("x", "y")
gridded(tmpgrid) = ~x+y
# set projection to lon (x) & lat (y), i.e. unprojected;
# find projections with Terminal: proj -lp
proj4string(tmpgrid) = CRS("lonlat")
# Fit a variogram model to the empirical variogram
# for single variogram
sp1_vgm = fit.variogram(vg, model=default_vgm)
plot(vg, model=default_vgm)
plot(vg, model=sp1_vgm)
# simple kriging
sk = krige(sp1~1, tmpdata, tmpgrid, sp1_vgm, nsim=0,
indicators = TRUE, nmax = 40)
# plot the result
print(spplot(sk, cuts=1, col.regions=grey.colors(2, 0.6,
0.90, 2.2)))
print(spplot(sk))
alphaChannelSupported = function()
{
!is.na(match(names(dev.cur()), c("pdf")))
}
spplot_hits = list("sp.points", hits, pch=19, col="black",
alpha = ifelse(alphaChannelSupported(), .5, 1))
spplot_misses = list("sp.points", misses, pch=21, cex=0.75,
col="black", alpha = ifelse(alphaChannelSupported(), .5, 1))
# col.regions does color scale for grid
# sp.layout contains a list of the various points, text,
etc. to plot
# at=seq() does limits of the color bar
spplot(sk["var1.pred"], scales=list(draw = TRUE),
col.regions=rev(rainbow(100)[1:75]),
sp.layout=list(spplot_hits, spplot_misses), at=seq(0, 1, 1/75))
=================================
--
====================================================
Nicholas J. Matzke
Ph.D. Candidate, Graduate Student Researcher
Huelsenbeck Lab
Center for Theoretical Evolutionary Genomics
4151 VLSB (Valley Life Sciences Building)
Department of Integrative Biology
University of California, Berkeley
Graduate Student Instructor, IB200A
Principles of Phylogenetics: Systematics
http://ib.berkeley.edu/courses/ib200a/index.shtml
Lab websites:
http://ib.berkeley.edu/people/lab_detail.php?lab=54
http://fisher.berkeley.edu/cteg/hlab.html
Dept. personal page:
http://ib.berkeley.edu/people/students/person_detail.php?person=370
Lab personal page:
http://fisher.berkeley.edu/cteg/members/matzke.html
Lab phone: 510-643-6299
Dept. fax: 510-643-6264
Cell phone: 510-301-0179
Email: matzke at berkeley.edu
Mailing address:
Department of Integrative Biology
3060 VLSB #3140
Berkeley, CA 94720-3140
-----------------------------------------------------
"[W]hen people thought the earth was flat, they were wrong.
When people thought the earth was spherical, they were
wrong. But if you think that thinking the earth is spherical
is just as wrong as thinking the earth is flat, then your
view is wronger than both of them put together."
Isaac Asimov (1989). "The Relativity of Wrong." The
Skeptical Inquirer, 14(1), 35-44. Fall 1989.
http://chem.tufts.edu/AnswersInScience/RelativityofWrong.htm
More information about the R-sig-Geo
mailing list