2

I am trying to replicate the example provided in the micromap R package of a linked map with the poverty rate for the 50 US states. I am trying to achieve the same results with a Shapefile from Germany. Here is my code:

library(micromap)
library(ggplot2)

setwd ('C:/Users/Jesus/Dropbox/linked maps/Chapter4')
ger<-readShapePoly("germany3.shp")
edPov<-read.csv('gerpoverty.csv')

statePolys <- create_map_table(ger, 'VARNAME_1')
head(statePolys)
lmplot(stat.data=edPov, 
       map.data=statePolys, 
       panel.types=c('labels', 'dot','map'),
       panel.data=list('Id1','poverty',NA),
       ord.by='poverty',
       grouping=5, median.row=F,
       map.link=c('Id1','VARNAME_1'))

When I tried to parse it into a flat table for use with ggplot2 the procedure does not work (instead of having 16 observations it has 8000 plus observations). Also when I try to plot the linked map, the following error appears on screen:

Error in `[.data.frame`(DF, , ord.by) : undefined columns selected

Here is the link to the files in a zip file:

https://www.dropbox.com/s/c43k755aadvu2z6/germany.rar

1

Some problems with your code were:

  1. You did not have information on your .csv file about "poverty" like the edPov dataset from micromap package has.
  2. The second value ("Varname_1") inside the argument map.link is not correct. You need to specify a column from the statePolys object, i.e., in that case it would be the "ID" column to match with the Id1 column in the edPov object.

So, to provide an example I added a column (column S) into the gerpoverty.csv file named "poverty".
I made up the poverty values (it was a range from #16 in the first row to #1 in the last row).


Here is the code I used:

library(micromap)
library(rgdal)

#read/import the germany3.shp shapefile in R. 
#the dsn argument is the directory and the layer argument is the shapefile name.
germany <- readOGR(dsn = "C:\\..", layer = "germany3")

#read gerpoverty.csv file from the directory it is stored.
edPov = read.csv("C:\\...\\gerpoverty.csv", sep=",")

statePolys <- create_map_table(germany, 'VARNAME_1')

lmplot(stat.data=edPov,         
       map.data=statePolys,
       panel.types=c('labels','dot','map'),
       panel.data=list('Id1','poverty', NA),
       ord.by='poverty',
       grouping=4, median.row=FALSE,
       map.link=c('Id1','ID'))

enter image description here

  • Thanks this is very helpful, and yes there was something wrong with the csv file. – Jlkm Feb 10 '14 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.