Choropleth map with R using shp file

Hi community stackoverflow!

Can someone kindly help me, as I am having some difficulty creating a choropleth map in R. At the moment, I have assigned LL information to my interests, and now I want to create a choropleth map using "cans" in the data set (data .csv) by high school district (highdist_n83.shp.zip). I would like you to know how to correctly fill in the card with the amount of cans in the district square. I have provided code that extracts an example data file from dropbox and a form file that I would like to use.

EDIT Sorry, I forgot to add that when I draw only the form file, I can see how it is displayed through ggplot. However, when I try to β€œfill” the areas using the number of β€œcans” variables, R hangs for a while before displaying what appears to be a mass of lines above the original shape. I wonder if an error occurs due to the following possible reasons.

  • The form file is not good.
  • There may be a problem with the way I combine the data frame and the form file, as I noticed that the added lines are added to the combined file
  • There are several schools in the area that I have not combined when using ddply.

Thank you for your time!

###load R scripts from dropbox dropbox.eval <- function(x, noeval=F) { require(RCurl) intext <- getURL(paste0("https://dl.dropboxusercontent.com/",x), ssl.verifypeer = FALSE) intext <- gsub("\r","", intext) if (!noeval) eval(parse(text = intext), envir= .GlobalEnv) return(intext) } ##pull scripts from dropbox dropbox.eval("s/wgb3vtd9qfc9br9/pkg.load.r") dropbox.eval("s/tf4ni48hf6oh2ou/dropbox.r") ##load packages pkg.load(c(ggplot2,plyr,gdata,sp,maptools,rgdal,reshape2)) ###setup data frames dl_from_dropbox("data.csv","dx3qrcexmi9kagx") data<-read.csv(file='data.csv',header=TRUE) ###prepare GIS shape and data for plotting dropbox.eval("s/y2jsx3dditjucxu/dlshape.r") temp <- tempfile() dlshape(shploc="http://files.hawaii.gov/dbedt/op/gis/data/highdist_n83.shp.zip", temp) shape<- readOGR(".","highdist_n83") #HDOE high school districts shape@proj4string 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 shape2.df<-fortify(shape2) mshape2.df<- merge(shape2.df, shape2@data , 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") print(mapPlot) 
+4
source share
1 answer

When you strengthen the shapefile, you get the wrong identifiers. Instead, explicitly add an identifier column based on the line names of the form file and use it to combine. You also don't tell geom_polygon how to group the rows in data.frame so that they are built as one continuous overlapping, self-intersecting polygon. Add a group argument to geom_polygon . I would also like to use RColorBrewer to select nice colors for maps (using the brewer.pal function).

Try the following:

 require(RColorBrewer) shape2@data $id <- rownames( shape2@data ) sh.df <- as.data.frame(shape2) 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" ) )+ coord_equal() 

enter image description here

+7
source

Source: https://habr.com/ru/post/1485758/


All Articles