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

Muenchen, Robert A (Bob) muenchen at utk.edu
Mon Nov 1 21:53:18 CET 2010


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)
  
# 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")

=========================================================
  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
========================================================



More information about the R-sig-Geo mailing list