1

I am new to R and having a bit of trouble working with two sp data frames.

Data Frames:

  • 'violence' (which is a data frame containing instances of violent occurrences, listed by longitude and latitude)
  • 'DRC_GEO2' (which is shapefile that includes the administrative boundaries of the DRC).

I want to calculate the number of violent occurrences per administrative territory as defined by the DRC_GEO2 data frame polygons. This post (https://gis.stackexchange.com/questions/110117/counting-number-of-points-in-polygon-using-r/110246#110246?newreg=5bfe0d571b8241158bac704f188cf026) was helpful in outlining a similar issue. However, I do not want to record a sample of data points from "violence", I want all recorded instances in the data frame.

I have tried the below code which does output the correct # of instances, however, it classifies them according to the largest administrative unit ('ADM1_FR') but I would like it to classify by the smallest admin unit ('ADM2_FR').

res <- over(violence_point, DRC_GEO2)

table(res$NAME_1)

Which has returned: enter image description here

Ultimately, I would like a choropleth map of the number of instances of violence over the original shapefile 'DRC_GEO2'. How do I add the number of occurrences per administrative territory to the data in order to make the choropleth map?

1 Answers1

0

It is very difficult to share an exact answer without an MRE (see How to make a great R reproducible example).

That being said, here is a general answer to how you might accomplish this. I'd be happy to edit to be more specific if you are able to share data.

library(ggplot2)

# Count points in each ADM2_FR
res <- table(over(violence_point, DRC_GEO2[,"ADM2_FR"]))
names(res) <- c("ADM2_FR", "Violence_Count")

# Merge counts with original data
dat <- merge(DRC_GEO2, res, by = "ADM2_FR", all.x = TRUE)

# Plot choropleth 
ggplot() +
  geom_polygon(data = dat, aes(fill = Violence_Count, x = long, y = lat, group = group)) +
  theme_void() +
  coord_map()

See here: https://www.r-graph-gallery.com/327-chloropleth-map-from-geojson-with-ggplot2.html

Skaqqs
  • 4,010
  • 1
  • 7
  • 21