spplot help, please

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

spplot help, please

R-sig-geo mailing list
Hi,
I wanted to plot the spplot of my model residuals. The plot works fine, but the state map does not appear. Code is given below and the data are attached. I would greatly appreciate any help in this.

Marvin


library(spgwr)
library(spdep)
library(maps)
library(maptools)
data<-read.csv("try.csv",header=TRUE)
coords=cbind(data$Longitude,data$Latitude)
g.adapt.gauss <- gwr.sel(y~x, data,coords,adapt=TRUE)
res.adpt <- gwr(y~x, data,coords, adapt=g.adapt.gauss)
brks <- c(-0.25, 0, 0.01, 0.025, 0.075)
cols <- grey(5:2/6)
res.adpt$SDF$ols.e <- residuals(lm(y~x, data))
spplot(res.adpt$SDF, c("ols.e","gwr.e"),main="Residuals")


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

try.csv (11K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: spplot help, please

Bede-Fazekas Ákos
Hi Marvin,

this code works fine just as it should work. Which "state map" you need?
A basemap with terrain/roads/cities/borders? In this case there are
several possibilities to do that. Have a look at these links, they may
solve your problem:
https://pakillo.github.io/R-GIS-tutorial/#gmap
http://www.nickeubank.com/wp-content/uploads/2015/10/RGIS3_MakingMaps_part1_mappingVectorData.html 
(Section "4.2 Basemaps with spplot")

HTH,
Ákos Bede-Fazekas
Hungarian Academy of Sciences


2017.10.21. 3:29 keltezéssel, Marvin Sharma via R-sig-Geo írta:

> Hi,
> I wanted to plot the spplot of my model residuals. The plot works
> fine, but the state map does not appear. Code is given below and the
> data are attached. I would greatly appreciate any help in this.
>
> Marvin
>
>
> library(spgwr)
> library(spdep)
> library(maps)
> library(maptools)
> data<-read.csv("try.csv",header=TRUE)
> coords=cbind(data$Longitude,data$Latitude)
> g.adapt.gauss <- gwr.sel(y~x, data,coords,adapt=TRUE)
> res.adpt <- gwr(y~x, data,coords, adapt=g.adapt.gauss)
> brks <- c(-0.25, 0, 0.01, 0.025, 0.075)
> cols <- grey(5:2/6)
> res.adpt$SDF$ols.e <- residuals(lm(y~x, data))
> spplot(res.adpt$SDF, c("ols.e","gwr.e"),main="Residuals")
>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spplot help, please

edzer
In reply to this post by R-sig-geo mailing list


On 10/21/2017 03:29 AM, Marvin Sharma via R-sig-Geo wrote:
> Hi,
> I wanted to plot the spplot of my model residuals. The plot works fine,
> but the state map does not appear. Code is given below and the data are
> attached. I would greatly appreciate any help in this.

I can't see where you're trying to plot/add a state map, in the example
below.

>
> Marvin
>
>
> library(spgwr)
> library(spdep)
> library(maps)
> library(maptools)
> data<-read.csv("try.csv",header=TRUE)
> coords=cbind(data$Longitude,data$Latitude)
> g.adapt.gauss <- gwr.sel(y~x, data,coords,adapt=TRUE)
> res.adpt <- gwr(y~x, data,coords, adapt=g.adapt.gauss)
> brks <- c(-0.25, 0, 0.01, 0.025, 0.075)
> cols <- grey(5:2/6)
> res.adpt$SDF$ols.e <- residuals(lm(y~x, data))
> spplot(res.adpt$SDF, c("ols.e","gwr.e"),main="Residuals")
>
>
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Edzer Pebesma
Institute for Geoinformatics
Heisenbergstrasse 2, 48151 Muenster, Germany
Phone: +49 251 8333081

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: spplot help, please

Florian Detsch
The others are right, there is no state map in your code that you might
add to the points plot.

After a quick inspection of which geographic area we are dealing with,
here is one possible - and quite basic - solution on how to add state
polygons of the US (if that is what you intend to do) to your plot:

## download state polygons for the us
library(raster)
usa = getData("GADM", country = "USA", level = 1)

## create subset of state polygons (otherwise spplot() will take quite
long)
proj4string(res.adpt$SDF) = "+init=epsg:4326"

ext = extend(extent(res.adpt$SDF), 2.5)
states = crop(usa, ext)

## add state boundaries using 'sp.layout'
p = spplot(res.adpt$SDF, c("ols.e","gwr.e"), main = "Residuals",
            sp.layout = list("sp.polygons", states))
p

Alternatively, you could also use latticeExtra::layer() instead of
'sp.layout'. Simply have a look at the help pages for further details.

Cheers,
Florian


Am 21.10.2017 um 10:58 schrieb Edzer Pebesma:

>
> On 10/21/2017 03:29 AM, Marvin Sharma via R-sig-Geo wrote:
>> Hi,
>> I wanted to plot the spplot of my model residuals. The plot works fine,
>> but the state map does not appear. Code is given below and the data are
>> attached. I would greatly appreciate any help in this.
> I can't see where you're trying to plot/add a state map, in the example
> below.
>
>> Marvin
>>
>>
>> library(spgwr)
>> library(spdep)
>> library(maps)
>> library(maptools)
>> data<-read.csv("try.csv",header=TRUE)
>> coords=cbind(data$Longitude,data$Latitude)
>> g.adapt.gauss <- gwr.sel(y~x, data,coords,adapt=TRUE)
>> res.adpt <- gwr(y~x, data,coords, adapt=g.adapt.gauss)
>> brks <- c(-0.25, 0, 0.01, 0.025, 0.075)
>> cols <- grey(5:2/6)
>> res.adpt$SDF$ols.e <- residuals(lm(y~x, data))
>> spplot(res.adpt$SDF, c("ols.e","gwr.e"),main="Residuals")
>>
>>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>

--
Dr. Florian Detsch
Environmental Informatics
Department of Geography
Philipps-Universität Marburg
Deutschhausstraße 12
35032 (parcel post: 35037) Marburg, Germany

Phone: +49 (0) 6421 28-25323
Web: http://www.uni-marburg.de/fb19/fachgebiete/umweltinformatik/detschf/index.html

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo