[R-sig-Geo] Bubble plot from SpatialPolygons object

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue Nov 2 08:05:13 CET 2010



On 11/01/2010 09:53 PM, Muenchen, Robert A (Bob) wrote:
> Hi All,
> 
> I'm having great fun learning the vast array of mapping options in R. I'm now working on a teaching example (below) that will step slowly through creating a tiny map, converting it to  SpatialPolygons and then plotting a choropleth and bubble plots in several ways (to be simplified to just one way when I'm done learning.)
> 
> The first place I'm stuck is getting a legend to handle both size and color at the same time on the bubble plot (2nd from bottom). Either one works by itself, but not both together.
> 
> My second problem is using plot twice with "add=TRUE" (bottom example). When I run the book example though it works fine, so I've got to be doing something truly inane.
> 
> Finally, I'm plotting the bubbles in the centers of the regions that I entered into a dataframe. That seems silly since these are created automatically when I convert from a map object to a SpatialPolygons object, and stored in the labpt slots. There must be an easy way to call them from there, but I haven't figured it out.
> 
> Thanks!
> Bob
> 
> Here's what I've got so far:
> 
> #----------------------------------------
> # Create a "map" class object of Tinyland
> #----------------------------------------
> long <- c(
> 20, 25, 32, 30, 27, 18, 20, NA,
> 35, 42, 42, 32, 25, 29, 35, NA,
> 30, 35, 35, 29, 25, 20, 20, 24, 30)
> lat <- c(
> 30, 30, 35, 39, 39, 34, 30, NA,
> 21, 25, 33, 35, 30, 23, 21, NA,
> 18, 18, 21, 23, 30, 30, 25, 20, 18)
> range <- c(18, 42, 18, 39)
> names=c("A","B","C")
> TinylandMap <- list(
>   x=long,
>   y=lat,
>   names=names,
>   range=range)
> class(TinylandMap) <- "map"
> 
> TinylandMap
> map(TinylandMap)
> 
> # Now convert it to a SpatialPolygon object
> 
> library("maptools")
> TinylandSP <- map2SpatialPolygons( TinylandMap, 
>   IDs=TinylandMap$names,
>   proj4string=CRS("+proj=longlat +ellps=WGS84") ) 
> str(TinylandSP, max.level=2)
> plot(TinylandSP)
> 
> # Create some "pop"ulation data
> region <- c("A","B","C")
> pop <- c(271828, 314159, 195996) 
> labptX <- c(25,34,26) #X-value of center of each region
> labptY <- c(34,28,23) #Y-value of center of each region
> myPopData <- data.frame(region,labptX,labptY,pop)
> myPopData
> 
> # Get some grey shades
> # could automate this: myGreys <- grey( (1-( pop / max(pop) ) ) ) 
> # but these look nicer:
> myGreys <- c("gray60","gray30","gray90")
> myGreys
> 
> # Choroploth plot from SpatialPolygons
> plot(TinylandSP, col = myGreys  )
> legend( 37, 38 ,
>   pop,
>   fill = myGreys  )
> text( 37, 38.5, pos=4, "pop")
> 
> # Create some Character EXpansion values
> myCexValues <- 5*pop/max(pop)
> 
> # This works and has a nice legend:
> library(PBSmapping)
> plot(TinylandSP)
> addBubbles(labptX, labptY, pop,
>   legend.type="nested", col="grey")
> 
> # This almost works but legend bubbles are not black
> # and overlap quite a bit. Is the legend function
> # capable of making the points black?
> 
> plot(TinylandSP)
> symbols(labptX, labptY, circles=pop, inches=1/5, bg="black", add=TRUE)
> legend( 37, 38, legend=pop, 
>   pch=1, pt.cex=myCexValues)

use

pch = 16, y.intersp=2

in the legend command.

> # Here I'm trying to follow the example from 
> # Applied Spatial Data Analysis with R
> # page 67 bottom by using plot twice.
> # But the 2nd plot says "add is not a graphical parameter"
> # so the bubbles start over on a blank map
> # I'm also trying fill="black" in the legend & getting squares
> 
> plot(TinylandSP)
> plot( labptX, labptY, pch=1, bg="black", cex=myCexValues, add=TRUE )
> legend( "topright", legend=pop, 
>   pch=1, pt.cex=myCexValues, fill="black")

instead of the second plot, use:

points(labptX, labptY, pch=1, bg="black", cex=myCexValues)
> 
> =========================================================
>   Bob Muenchen (pronounced Min'-chen), Manager  
>   Research Computing Support
>   Voice: (865) 974-5230  
>   Email: muenchen at utk.edu
>   Web:   http://oit.utk.edu/research, 
>   News:  http://oit.utk.edu/research/news.php
> ========================================================
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list