Reputation: 442
I'm trying to add points to a plot drawn with the spplot() function. I've already read this links, but I haven't solved yet.
Overplotting two SpatialPolygonsDataFrames with spplot
plotting SpatialPointsDataFrame over a SpatialPolygonsDataFrame
http://www.r-bloggers.com/label-placement-with-spplot-and-lattice/
I have the results of a kriging stored in a variable named "k.o", I want to create a map with the prediction obtained with kriging and add the points where the measures were taken. That's my code:
stations<-list('sp.points', zone, cex= 1, pch=19, col='black')
prediction_plot<-spplot(k.o["var1.pred"], col.regions=terrain.colors(20), cuts=19, auto.key=list(draw=TRUE),
asp='iso', colorkey=TRUE, main='Mean temperature prediction: january', sub='Lombardy',
scales=list(draw=TRUE), sp.layout=list(stations))
print(prediction_plot)
With this code the compiler gives me no error, but I don't see the points overplotted (i.e I see only kriging prediction).
I tried also with points(), with no results (but I'm not sure I can use points() with spplot()):
prediction_plot<-spplot(k.o["var1.pred"], col.regions=terrain.colors(20), cuts=19, auto.key=list(draw=TRUE),
asp='iso', colorkey=TRUE, main='Mean temperature prediction: january', sub='Lombardy',
scales=list(draw=TRUE))
points(zone$long, zone$lat, type='p', cex=0.5, col=2)
print(prediction_plot)
Sorry, I was forgetting: 'zone' is of Formal Class SpatialPointsDataFrame.
Hope this question is clear enough,
thanks in advance
Edit: I think it's rstudio it's drawing the plot of the krige over the points, so I tried with the option "first" of sp.layout.
stations<-list('sp.points', zone, cex= 0.5, pch=19, col='black')
prediction_plot<-spplot(k.o['var1.pred'], col.regions=terrain.colors(20), cuts=19, auto.key=list(draw=TRUE),
asp='iso', colorkey=TRUE,
scales=list(draw=TRUE), sp.layout=list(stations, first=FALSE))
print(prediction_plot)
Now it draws me the points, but not the krige prediction, and on the plot, further the points, it prints an error message that says:"Error using packet 1. $ operator is invalid for atomic vectors". I searched and found that, for avoiding such an error, it is necessary to use [ ] instead of $, but I'm already using them, with k.o['var1.pred']...
Any suggestion? Thanks!
Upvotes: 0
Views: 1981
Reputation: 4121
This is a bug in spplot
that will be corrected when sp
version 1.0-18 appears (now corrected in svn, on r-forge). Usually, kriged results are gridded (SpatialPixelsDataFrame
or SpatialPointsDataFrame
) and not SpatialPointsDataFrame
, for gridded data you won't see this problem.
Upvotes: 1