-1

Im reading in spatial data from a shapefile that contains the location of 100 districts in a province. In another dataframe, I have each districts population data.

Im trying to use ggplot to create a map of the province (from the shapefile) and color each district based on population size. The issue here is that I don't know how to fill the map garnered by the shapefile with colors based on the districts corresponding population values that are contained in another dataframe.

Note that I understand the code below doesn't work but it illustrates what Im intuitively trying to accomplish

districts_from_shapefile <- readOGR("districts.shp")
districts_with_pop <- read.csv(file = "districts_populations.csv")

ggplot() +  
        geom_polygon(data=districts_from_shapefile, aes(x=long,y=lat,group=group)) +
        geom_polygon(data=districts_with_pop, aes(fill=districts_with_pop$population))

Thanks!

aport550
  • 119
  • 1
  • 9
  • There are many tutorials out there. For example, how about [this one](https://rstudio-pubs-static.s3.amazonaws.com/324400_69a673183ba449e9af4011b1eeb456b9.html)? – jazzurro Jan 10 '20 at 08:50
  • If you can provide the data in `dput()` format and the shapefile I can help you out. – UseR10085 Jan 10 '20 at 08:55
  • You can have a look at this https://stackoverflow.com/a/59588816/6123824 – UseR10085 Jan 10 '20 at 09:08

1 Answers1

0

Make sure your csv has the same number of rows and same districts in the same order as the shapefile. cbind the population column from the csv to the shapefile attributes. Then you just use geom_polygon once, and fill=population is an additional aes (besides x, y, group).

If the rows don't match you could use dplyr::join to get the population column over.

Jim Worrall
  • 466
  • 3
  • 11