rworldmap : world maps from country data Andy South southandy@gmail.com andysouth.co.uk CambridgeR, November 27th 2014 1 2 What do you want to say with your global data ? 1. Geographical pattern 2. Identify key countries quickly 3. Ranking 4. Relative values 5. Extremes 3 rworldmap • Released 2010 • Aims improve visualisation of global data in R easy to start, flexible to extend publication quality outputs aid interdisciplinary work • ~50 downloads per day via Rstudio mirror • Contains up-to-date country boundaries • RJournal article 4 The R Journal Vol. 3/1, June 2011 http://journal.r-project.org/archive/2011-1/RJournal_2011-1_South.pdf 5 rworldmap shiny UI https://andysouth.shinyapps.io/rworldmapUI/ 6 rworldmap uses package sp • spatial classes • Spatial Polygons Dataframe (sPDF) @data 7 maps in rworldmap are Spatial Polygons Dataframes sPDF <- getMap() class(sPDF) plot(sPDF) #to get at the dataframe dF <- sPDF@data str(dF) 8 vector raster 9 Joining 1 Your data Map data country temperature UK Spain cold hot Columns in common 10 Joining 2 dF <- data.frame( country=c("spain", "uk"), weather=c("hot", "cold") ) sPDF <- joinCountryData2Map( dF, joinCode = "NAME", nameJoinColumn = "country") mapCountryData( sPDF, nameColumnToPlot= "weather") 11 Note that these data are categorical & rworldmap copes. 12 HOWEVER … Joining using country codes instead of country names avoids potential pitfalls. 13 14 • North Korea Korea, Democratic People's Republic of Democratic People's Republic of Korea Korea, Dem. Rep. • South Korea Korea, Republic of Republic of Korea Korea 15 Côte d'Ivoire was my bête noire 16 17 Joining data using country codes #example data in rworldmap data(countryExData) sPDF <- joinCountryData2Map( countryExData, joinCode = "ISO3", nameJoinColumn = "ISO3V10") mapParams <- mapCountryData(sPDF, nameColumnToPlot="EPI") 18 Examples with world bank indicators library(WDI) # http://data.worldbank.org/indicator/ # Improved sanitation facilities (% of popn) indicator <- "SH.STA.ACSN" dFsanitation <- WDI( indicator=indicator, start=2005, end=2005 ) 19 i. choropleth map library(rworldmap) sPDFsanitation <joinCountryData2Map( dFsanitation, nameJoinColumn= "iso2c", joinCode='ISO2') library(RColorBrewer) numCats <- 5 colourPalette <- rev(brewer.pal(numCats, "PuBuGn")) mapCountryData( sPDFsanitation, nameColumnToPlot=indicator, colourPalette=colourPalette, numCats=numCats ) 20 ii. bubble map library(rworldmap) mapBubbles( sPDFsanitation , nameZSize = indicator , nameZColour = 'blue' , fill = FALSE ) #optional interactive labelling of countries identifyCountries( sPDFsanitation , nameColumnToPlot = indicator ) 22 iii. Ranked bar plot library(rworldmap) barplotCountryData( sPDFsanitation , nameColumnToPlot=indicator , nameCountryColumn = "NAME" , numCats = numCats , colourPalette = colourPalette , na.last = NA , decreasing = FALSE , scaleSameInPanels = TRUE , numPanels = 7 , cex = 1.1 ) 24 Access to sanitation from barplotCountryData() non world maps ## US states map downloaded from : ## http://www2.census.gov/cgibin/shapefiles2009/national-files inFile <- 'tl_2009_us_state.shp' sPDF <- readShapePoly(inFile) str(sPDF@data) ## use mapPolys to map the sPDF mapPolys(sPDF,nameColumnToPlot = "AWATER“ ,mapRegion='North America') 26 non world maps 27 • These are the basics of rworldmap • Add bits together to make more interesting plots … 28 http://stackoverflow.com/a/19695755/1718356 29 https://alienplantation.wordpress.com/2013/06/18 /nuts-and-bolts-modern-maps-of-eurasia-in-r/30 Things to remember • spatial polygons dataframes • joining country data to maps • country codes • rworldmap::barplotCountryData() 31 rworldmap positives • modern maps (including South Sudan) • matches multiple names for same country & reports on failures in country matching • country codes (ISO 2 letter, 3 letter & numeric) • maps at 3 resolutions • copes with map projections and holes • flexibility 32 https://andysouth.shinyapps.io/shinytse7/ 33 Meta-mapping (using rworldmap to map the downloads of …) southandy@gmail.com andysouth.co.uk 34 Xtra bits after here … 35 modifying map legends data(countryExData) sPDF <- joinCountryData2Map(countryExData , joinCode = "ISO3" , nameJoinColumn = "ISO3V10") mapParams <- mapCountryData(sPDF , nameColumnToPlot="EPI" , addLegend=FALSE ) do.call( addMapLegend , c(mapParams , legendLabels="all" , legendWidth=0.5 )) 36 adding multiple elements to a plot mapCountryData() mapBubbles( getMap() , nameZSize="GDP_MD_EST" , nameZColour="black" , add=TRUE ) 37 interactive identification & labelling mapCountryData(nameColumnToPlot="POP_EST") identifyCountries(nameColumnToPlot="POP_EST") Then click close to the centre of countries to bring up country labels. 38 Resources http://andysouth.co.uk/softwarerworldmap/ https://groups.google.com/group/rworldmap 39 40
© Copyright 2025