Geocode Data - How to Use .kml File in R

Viewed 882

I've used some open source data to produce choropleth maps showing population density in the Republic of Ireland using the leaflet package in R.

Found a suitable .kml file which is based on the same geocodes as my table of population density (it's at "small area" level which splits up the country into about 19,000 chunks).

I have some code to import the .kml file:

ireland <- readOGR("Source Data/Small_Areas__Generalised_100m__OSi_National_Boundaries.kml")

and a data frame df_merged which has fields GEOGID and density (and a few others). The code for producing the map is:

popup1 <- paste0("<span style='color: #7f0000'><strong>Data</strong></span>",
                 "<br><span style='color: salmon;'><strong>Small Area: </strong></span>", df_merged$GEOGID, 
                 "<br><span style='color: salmon;'><strong>Population: </strong></span>", df_merged$T1_1AGETT,
                 "<br><span style='color: salmon;'><strong>Area: </strong></span>", df_merged$Shape__Area,
                 "<br><span style='color: salmon;'><strong>Density: </strong></span>", df_merged$density
)

mymap_density <- leaflet() %>%

  addProviderTiles("Esri.WorldGrayCanvas",
                   options = tileOptions(minZoom = 5,
                                         maxZoom = 18,
                                         opacity = 1)) %>%

  addPolygons(data = ireland,
              fillColor = ~palette_density(df_merged$density),
              fillOpacity = 0.5,
              color = "darkgrey",
              weight = 0.1,
              popup = popup1) %>%

  addLegend(position = "topleft",
            colors = my_color_scheme,
            labels = c("Lowest", "", "", "", "", "", "", "", "Highest"),
            opacity = 0.6,
            title = "Population Density")

The map draws fine, but there's a problem: it's fairly obvious that the data for a given geocode isn't appearing in the correct part of the map. For example, in the map below, the geocode A048053001 represents a small urban area in Cork but the data for this geocode is shown in a completely different place on the map. The map is telling me that a big empty area of countryside has a tiny area and an enormous population density, which is daft.

I don't really know anything about the innards of .kml files so I'm not sure how to go about fixing this problem. At no point in the code have I told R that the df_merged$GEOGID field should be associated with a particular polygon inside the .kml file - so have I missed out a stage or made a mistake here?

If I import the .kml file into Google Earth, this problem doesn't occur - which I think rules out any problems with the underlying data (which is supplied by the Irish government which ought to be a reliable source anyway).

The above code isn't really a reproducible example since it relies on external data files.

Can anyone offer any advice please?

Thank you.

enter image description here

0 Answers
Related