[R-sig-Geo] spplot with quantitative variable
Roger Bivand
Roger.Bivand at nhh.no
Thu Aug 13 11:55:54 CEST 2009
On Thu, 13 Aug 2009, Matthieu Stigler wrote:
> Hi
>
> I'm new to sptial analysis and just dicovers so I have a really basic
> question. I could unfortunately not find the answer on in the tuto,
> help files or springer book (tough it is sure there :-(). So sorry if
> the answer is trivila but this would help me a lot!
>
> I have a an object say TRY of class "SpatialPolygonsDataFrame"
> representing a country and its subareas (26). First question, is there
> a generic function to access the name of the subdivisions?
> I used the workaround: as.data.frame(TRY)[,"NAME_1"] is there
> something more elegant? How to extract the values in general?
> TRY[,"NAME_1"] prints a lot of information beside the series I want.
If the observation (feature) IDs are set to the name, row.names(TRY) would
work (for recent releases of sp). If they are not so set, you can set them
by:
TRY1 <- spChFIDs(TRY, as.character(TRY$NAME_1))
(see ?spChFIDs in maptools and examples in ch. 5). I suspect that NAME_1
is a factor - what does str(as(TRY, "data.frame")) say?
>
> My problem actually concerns to add a new series (say population) to
> this dataset, and to plot it...
>
> To add the series, I did:
> data$new<-rnorm(100)
Yes, if the object is TRY, then TRY$new <- will do it. See also ?spCbind
in maptools if you need more control of the ordering.
>
> is that the right manner?
>
> Then I tried to plot it using spplot() but I hvae the following
> problem: the colors are just sorted following the order in which the
> values appear in the series (so the first subdivion with name A...
> with low pop can have same color as subdivion B with high color). So
> the data is not sorted correspondly and the colors are totally messed
> up. I just want to have the colors "increasing " with the population.
> Furterhmore, my scale just shows the individual values for each
> subdivision...
This sounds very much as though the variable you are using is a factor,
which will generate one colour per factor level. Are you reading in data
that say use a comma as the decimal separator in a locale that expects a
point (or the reverse)? You can convert a wrongly identified factor to
numeric by coercing first to character, then sub()stituting the "," with
"." (or the reverse if necessary, be careful to escape the point as it has
special meanings in regular expressions), then coerce to numeric. This is
R FAQ 7.10:
http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f
for the conversion, see ?sub for changing the decimal separator.
Hope this helps,
Roger
>
> What shoulk I use? Define myself colors palette as in the springer
> book at section 3,4? Do I do something wrong when adding the variable?
> I don't understand why I dont get the same result as the example in
> the demo(gallery):
>
> library(sp); library(lattice) # required for trellis.par.set():
> trellis.par.set(sp.theme()) # sets color ramp to bpy.colors()
> library(maptools)
> nc <- readShapePoly(system.file("shapes/sids.shp",
> package="maptools")[1], proj4string=CRS("+proj=longlat +datum=NAD27"))
> spplot(nc, "SID79", names.attr = "1979")
>
> here the scale does not give the individual values as in my case. Are
> those object "SID79" and the series I entered so different? I can I
> caompre them?
>
> Thanks a lot!!
>
> Matthieu Stigler
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list