Choropleth map with R using shp file

Choropleth map with R using shp file

Hello stackoverflow community!

Could someone kindly please assist me as I am having some difficulties with creating a choropleth map in R. As of now, I have assigned LL information to my points of interest, and I now want to create a choropleth map using the “cans” variable in the dataset (data.csv) by high school district area ( What I would like to please know is how to properly fill the map with the sum of cans per district area. I have provided code, which pulls a sample datafile from dropbox and the shape file I would like to use.

Sorry, I forgot to add that when I plot just the shape file, I am able to see it rendered via ggplot. However, when I try to “fill” the districts using the number of “cans” variable, R hangs for awhile before rendering what appears to be a mass of lines over the original shape. I wonder if the error is due to the following possible reasons

  1. the shape file is not good
  2. there may be an issue with how I am merging the data frame and the shape file as I’ve noticed additional rows are added to the merged file
  3. there are multiple schools in an area, which I haven’t combined when using ddply.

Thank you for your time!

###load R scripts from dropbox
dropbox.eval <- function(x, noeval=F) {
intext <- getURL(paste0("",x), ssl.verifypeer = FALSE)
intext <- gsub("r","", intext)
if (!noeval) eval(parse(text = intext), envir= .GlobalEnv)

##pull scripts from dropbox 

##load packages

###setup data frames

###prepare GIS shape and data for plotting
temp <- tempfile()
dlshape(shploc="", temp)
shape<- readOGR(".","highdist_n83") #HDOE high school districts  
[email protected] 

shape2<- spTransform(shape, CRS("+proj=longlat +datum=NAD83"))

data.2<-ddply(data, .(year, schoolcode, longitude, latitude,NAME,HDist,SDist), summarise,
        total = sum(total),
        cans= sum(cans))

coordinates(data.2) <-~longitude + latitude
mshape2.df<- merge(shape2.df,[email protected], by.x="id", by.y="ID",all=TRUE)

newdata <- merge(mshape2.df,data.2, by.x="NAME", by.y="NAME", all=TRUE)
newdata  <- newdata [order(newdata $order),]

###choropleth map: 
mapPlot <- ggplot(newdata,aes(x=long, y=lat,drop=FALSE)) +
geom_polygon(aes(fill=cans, drop=FALSE), colour = "black", lwd = 1/9,na.rm=FALSE)
+ ggtitle("Total of Cans Across State")

When you fortify your shapefile you end up with the wrong IDs. Instead, explicitly add an ID column based on the row names of the shapefile and use this to merge. You also do not tell geom_polygon how to group the rows in your data.frame so they are all plotted as one continuous overlapping, self-intersecting polygon. Add a group argument to geom_polygon. I also like to use RColorBrewer to pick nice colours for maps (using the function brewer.pal).

Try this:

[email protected]$id <- rownames([email protected])
sh.df <-
sh.fort <- fortify(shape2 , region = "id" )
sh.line<- join(sh.fort, sh.df , by = "id" )

mapdf <- merge( sh.line , data.2 , by.x= "NAME", by.y="NAME" , all=TRUE)
mapdf <- mapdf[ order( mapdf$order ) , ]

ggplot( mapdf , aes( long , lat ) )+
  geom_polygon( aes( fill = cans , group = id ) , colour = "black" )+
  scale_fill_gradientn( colours = brewer.pal( 9 , "Reds" ) )+

enter image description here