Learn R Programming

rworldmap (version 1.01)

mapPolys: Map polygon data.

Description

Plot a map of polygons, from a spatialPolygonsDataFrame, coloured according to one a specified attribute column.

Usage

mapPolys( mapToPlot = ""
        , nameColumnToPlot = ""
        , numCats = 7
        , xlim=NA
        , ylim=NA
        , mapRegion = "world"
        , catMethod="quantiles"
        , colourPalette= "heat"
        , addLegend=TRUE
        , borderCol = 'grey'
        , mapTitle = 'columnName'
        , oceanCol=NA
        , aspect=1
        , missingCountryCol=NA
        , add=FALSE                                                    
        )

Arguments

mapToPlot
a spatial polygons dataframe (e.g. from joinData2Map()) containing polygons and associated data, if none specified an internal example data is used
nameColumnToPlot
name of column containing the data you want to plot
numCats
number of categories to put the data in, may be modified if this number is incompatible with the catMethod chosen
xlim
map extents c(west,east), can be overidden by mapRegion
ylim
map extents c(south,north), can be overidden by mapRegion
mapRegion
a country name from getMap()[['NAME']] or 'world','africa','oceania','eurasia','uk' sets map extents, overrides xlim,ylim
catMethod
for categorisation of data "pretty", "fixedWidth", "diverging", "logfixedWidth","quantiles","categorical", or a numeric vector defining breaks
colourPalette
string describing the colour palette to use, choice of:
"palette" for the current palette a vector of valid colours, e.g. =c('red','white','blue') or output from RColourBrewer

Value

  • invisibly returns a list containing the data and main options used for the map, the list can be passed to addMapLegend or addMapLegendBoxes along with additional options to allow greater flexibility in legend creation.

item

  • addLegend
  • borderCol
  • mapTitle
  • oceanCol
  • aspect
  • missingCountryCol
  • add

Details

Certain catMethod and colourPalette options go well together. e.g. "diverging" and "diverging", "categorical" and "rainbow" There are two styles of legend available. If catMethod='categorical' or the packages fields and spam are not installed a simple legend with coloured boxes is created. Otherwise a colour bar legend is created. Finer control can be achieved by addMapLegendBoxes or addMapLegend repectively.

See Also

joinData2Map, classInt, RColorBrewer

Examples

Run this code
## this example uses downloaded files
## to run it download the files
## and remove the comment symbols '#' from all the lines starting with a single '#'

## US states map downloaded from :
## http://www2.census.gov/cgi-bin/shapefiles2009/national-files

#inFile <- 'tl_2009_us_stateec.shp'
#sPDF <- readShapePoly(inFile)
#str(sPDF@data)

##################
## use mapPolys to map the sPDF
#mapPolys(sPDF,nameColumnToPlot = "ALANDEC")
#mapPolys(sPDF,nameColumnToPlot = "AWATEREC",mapRegion='North America')

##################
## join some other data to it
## education data downloaded from here as xls then saved as csv
## http://nces.ed.gov/ccd/drpcompstatelvl.asp

#dataFile <- 'SDR071A_xls.csv'
#dF <- read.csv(dataFile,as.is=TRUE)
#str(dF)
## STATENAME
## DRP912 Dropout Rate, Grades 9 through 12

## joining the data to the map
## based upon state names (column NAMEEC in map, and STATENAME in the data)
#sPDF2 <- joinData2Map(dF
#        , nameMap = sPDF
#        , nameJoinIDMap = "NAMEEC"
#        , nameJoinColumnData = "STATENAME")

#################
## plot one of the attribute variables
#mapDevice()# to set nice shape map window
#mapPolys(sPDF2,nameColumnToPlot = "DRP912",mapRegion='North America')


#################
###to map US counties data (Tiger) downloaded from :
##http://www2.census.gov/cgi-bin/shapefiles2009/national-files

#inFile <- 'tl_2009_us_county.shp'
#sPDF <- readShapePoly(inFile)
#str(sPDF@data)
#mapPolys(sPDF,nameColumnToPlot='AWATER',xlim=c(-140,-65), ylim=c(25,45))

Run the code above in your browser using DataLab