unable to customize a plot produced from a shapefile

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

unable to customize a plot produced from a shapefile

satishr
Hi,
  I read ESRI shapefile and related files using readOGR, and was able to plot successfully but am unable to customize the plot. The data is attached and corresponds to US climate divisions.
  The below mentioned second command supposed to produce a plot of sub region of the USA but it's not. I tried using readShapePoly but no success. In addition, I found a lot of space on top of the picture and want to reduce it, but not sure how to do it. I request you to provide suggestions.
Thanks,
Satish


> climagdiv = readOGR("US102ClimateDivisions", "US102ClimateDivisions")
OGR data source with driver: ESRI Shapefile
Source: "US102ClimateDivisions", layer: "US102ClimateDivisions"
with 102 features and 5 fields
Feature type: wkbPolygon with 2 dimensions
> plot(climagdiv)
> plot(climagdiv,ylim=c(20,40))


>climagdiv = readShapePoly("US102ClimateDivisions.shp")
> plot(climagdiv)
> plot(climagdiv,ylim=c(20,40))

Data:
US102ClimateDivisions.zip
Reply | Threaded
Open this post in threaded view
|

Re: unable to customize a plot produced from a shapefile

Alexandre Villers-2
Hi,

You should have a look at the documentation (e.g. manuals) available on
graphics in R (on the CRAN website, hhttp://cran.r-project.org/) to
learn how to set the parameters of a R graphic, and more specifically at
the spplot function (from package sp) for spatial objects (the ASDAR
book of Roger Bivand et al. is worth the look  
http://www.springer.com/public+health/book/978-0-387-78170-9 ).
There are also several web sites showing this: just google "plot spatial
data r" and you should find what you are looking for.

HTH


Alex


Le 09/02/2012 22:19, satishr a écrit :

> Hi,
>    I read ESRI shapefile and related files using readOGR, and was able to
> plot successfully but am unable to customize the plot. The data is attached
> and corresponds to US climate divisions.
>    The below mentioned second command supposed to produce a plot of sub
> region of the USA but it's not. I tried using readShapePoly but no success.
> In addition, I found a lot of space on top of the picture and want to reduce
> it, but not sure how to do it. I request you to provide suggestions.
> Thanks,
> Satish
>
>
>> climagdiv = readOGR("US102ClimateDivisions", "US102ClimateDivisions")
> OGR data source with driver: ESRI Shapefile
> Source: "US102ClimateDivisions", layer: "US102ClimateDivisions"
> with 102 features and 5 fields
> Feature type: wkbPolygon with 2 dimensions
>> plot(climagdiv)
>> plot(climagdiv,ylim=c(20,40))
>
>> climagdiv = readShapePoly("US102ClimateDivisions.shp")
>> plot(climagdiv)
>> plot(climagdiv,ylim=c(20,40))
> Data:
> http://r-sig-geo.2731867.n2.nabble.com/file/n7270579/US102ClimateDivisions.zip
> US102ClimateDivisions.zip
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/unable-to-customize-a-plot-produced-from-a-shapefile-tp7270579p7270579.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

_______________________________________________
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: unable to customize a plot produced from a shapefile

satishr
Hi,
   'ylim', is a graphical prameter and  in the plot command should do the
job, but did not. However, I did go through a few other links but all point
to either plot to spplot, and tried a few things but unable to develop the
plot that I want. I understand that it is a small trick but am unable to
figure it out (do I need to provide ylim in a different format?) Greatly
appreciate any help.
Thanks,
Satish

>test=readShapePoly( "**US102ClimateDivisions.shp")
> testS=as(testshpol,"SpatialPolygons")
> plot(testS)
> plot(testS,ylim=c(30,50))

plot(testS) and plot(testS,ylim=c(30,50)) are producing almost similar
graph.

> test_sp=SpatialPolygonsDataFrame("**US102ClimateDivisions.shp")
Error in stopifnot(length(Sr@polygons) == nrow(data)) :
  trying to get slot "polygons" from an object of a basic class
("character") with no slots

On Thu, Feb 9, 2012 at 3:39 PM, Alexandre Villers <
[hidden email]> wrote:

> Hi,
>
> You should have a look at the documentation (e.g. manuals) available on
> graphics in R (on the CRAN website, hhttp://cran.r-project.org/) to learn
> how to set the parameters of a R graphic, and more specifically at the
> spplot function (from package sp) for spatial objects (the ASDAR book of
> Roger Bivand et al. is worth the look  http://www.springer.com/**
> public+health/book/978-0-387-**78170-9<http://www.springer.com/public+health/book/978-0-387-78170-9>).
> There are also several web sites showing this: just google "plot spatial
> data r" and you should find what you are looking for.
>
> HTH
>
>
> Alex
>
>
> Le 09/02/2012 22:19, satishr a écrit :
>
>  Hi,
>>   I read ESRI shapefile and related files using readOGR, and was able to
>> plot successfully but am unable to customize the plot. The data is
>> attached
>> and corresponds to US climate divisions.
>>   The below mentioned second command supposed to produce a plot of sub
>> region of the USA but it's not. I tried using readShapePoly but no
>> success.
>> In addition, I found a lot of space on top of the picture and want to
>> reduce
>> it, but not sure how to do it. I request you to provide suggestions.
>> Thanks,
>> Satish
>>
>>
>>  climagdiv = readOGR("**US102ClimateDivisions", "US102ClimateDivisions")
>>>
>> OGR data source with driver: ESRI Shapefile
>> Source: "US102ClimateDivisions", layer: "US102ClimateDivisions"
>> with 102 features and 5 fields
>> Feature type: wkbPolygon with 2 dimensions
>>
>>> plot(climagdiv)
>>> plot(climagdiv,ylim=c(20,40))
>>>
>>
>>  climagdiv = readShapePoly("**US102ClimateDivisions.shp")
>>> plot(climagdiv)
>>> plot(climagdiv,ylim=c(20,40))
>>>
>> Data:
>> http://r-sig-geo.2731867.n2.**nabble.com/file/n7270579/**
>> US102ClimateDivisions.zip<http://r-sig-geo.2731867.n2.nabble.com/file/n7270579/US102ClimateDivisions.zip>
>> US102ClimateDivisions.zip
>>
>> --
>> View this message in context: http://r-sig-geo.2731867.n2.**
>> nabble.com/unable-to-**customize-a-plot-produced-**from-a-shapefile-**
>> tp7270579p7270579.html<http://r-sig-geo.2731867.n2.nabble.com/unable-to-customize-a-plot-produced-from-a-shapefile-tp7270579p7270579.html>
>> Sent from the R-sig-geo mailing list archive at Nabble.com.
>>
>> ______________________________**_________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<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: unable to customize a plot produced from a shapefile

manuel.schneider
Try
plot(1, t="n", xlim=c(bbox(testS)[1,1], bbox(testS)[1,2]), ylim=c(30,50))
plot(testS, add=T)

Abw
Manuel

_______________________________________________
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: unable to customize a plot produced from a shapefile

Roger Bivand
Administrator
On Mon, 13 Feb 2012, Manuel Schneider wrote:

> Try
> plot(1, t="n", xlim=c(bbox(testS)[1,1], bbox(testS)[1,2]), ylim=c(30,50))
> plot(testS, add=T)

The issues here are quite complex. Firstly, you should have added a
coordinate reference system to the object (no direct impact on ylim, but a
contributing factor). Methods for plotting take an asp= argument for
aspect; in sp methods, the asp= is set to 1 for non-geographic
coordinates, and "stretched" for geographic coordinates depending on the
distance from the equator, see ?mapasp.

If the plot window is set with xlim or ylim, they will not respond
directly, but will create a rectangle of appropriate aspect, which means
that the plot region does not respect the x/ylim, but will expand to
values matching asp=. Next, read up on xaxs= and yaxs= in ?par; by
default, the axes are extended by 4% at each end, if x/yaxs="i", the
extension more or less respects the requested plot region. Then the
plotting devices clip to this region. So to get close to what you wanted,
you'd have to work out the geometry of the output rectangle, open a device
with the required shape, and go from there (using Manuel's contribution):

library(rgdal)
climagdiv = readOGR("US102ClimateDivisions", "US102ClimateDivisions")
proj4string(climagdiv) <- CRS("+proj=longlat +datum=WGS84")
x11(width=7, height=4)
plot(as(climagdiv, "Spatial"), xlim=bbox(climagdiv)[1,], ylim=c(20,40),
   xaxs="i", yaxs="i", axes=TRUE)
plot(climagdiv, add=TRUE)

or similar.

It is also possible to use rgeos:

library(rgeos)
SP <- SpatialPoints(rbind(c(-125, 25), c(-67, 40)),
   proj4string=CRS("+proj=longlat +datum=WGS84"))
clipbox <- gEnvelope(SP)
GI <- gIntersects(climagdiv, clipbox, byid=TRUE)
plot(climagdiv)
plot(climagdiv[GI[1,],], border="red", add=TRUE)

to choose Polygons objects at least partly within the chosen rectangle.
Not that this is not strictly appropriate for geographical coordinates,
and you may not want to clip either, as entities will appear strange in
the ouput graphics.

This isn't as simple as it appears, really.

Hope this clarifies,

Roger

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

--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway