0

I have a shapefile ("my_shapefile") in R.

Part 1: If I try inspecting this shapefile and check its contents by adding a "$" - there appears to be 3 parts:

head(my_shapefile$var1)
H1C Y8L K9H B6U
200 Levels: H1C Y8L K9H B6U ....

head(my_shapefile$var2)
[1] 55 75 65 45 
Levels: 15 20 25 30...

head(my_shapefile$var3)
[1] zone1 zone2 zone3
10 Levels: zone1 zone2 zone 3...

Part 2: When I inspect this shapefile using the "str" command, I get the following output:

    str(my_shapefile)
    
  .. ..$ : Formal Class 'Polygons' [package "sp"] with 5 slots:
.. .. .. ..@Polygons: List of 1
.. .. .. .. ..$ :Formal Class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. ..@labpt  : num[1:2] -64.4 44.3
.. .. .. .. ..@area   : num 0.00045
.. .. .. .. ..@hole   : logi FALSE
.. .. .. .. ..@ringDir : int 1
.. .. .. .. ..@coords : num[1:567, 1:2] -65.5, -65.5, -65.5, -65.5, 

.. .. .. .. ..@plotOrder: int 1
.. .. .. .. ..@ labpt: num[1:2] -64.4 44.3
.. .. .. .. ..@ ID: chr "98"
.. .. .. .. ..@ area: num 0.0045

Part 3: When I inspect this shapefile using "@", I get the following (sample) outputs:

my_shapefile@polygons


[[711]]
An object of class "Polygon"
Slot "labpt":
[1] -54.1881  49.5854

Slot "area":
[1] 4.845081e-05

Slot "hole":
[1] FALSE

Slot "ringDir":
[1] 1

Slot "coords":
            [,1]     [,2]
  [1,] -54.18229 49.58693
  [2,] -54.18233 49.58670
  [3,] -54.18218 49.58659

my_shapefile@plotOrder
 [1] 15 25 15 25

I have an aggregate dataset ("ag_data") that looks something like this (ag_data$var has the same levels as my_shapefile$var1):

head(ag_data)
  var count
1 H1C    15
2 Y8L    17
3 K9H    99
4 B6U    12

Is it possible to somehow "merge" this "ag_data" with "my_shapefile"?

I already created a "leaflet" map ("my_map") using some pre-existing data ("old_data") and the shapefile:

    library(leaflet)
# first make basic map
my_map = old_data %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(clusterOption=markerClusterOptions())

# next, add shapefile to the basic map
my_map %>% addPolygons(data = my_shapefile, weight = 5, col = 'blue')

Based on the "count" variable from "ag_data", I would like to modify the map to create a "choropleth"/heatmap with the following type of legend (where the "color/shading" is proportional to ag_data$count ):

enter image description here

I found this older post (Merging a Shapefile and a dataframe) in which a similar topic is discussed, but I am not sure if this is relevant.

  • Can someone please show me how I can merge the aggregate data to the shapefile, and then create this map/legend (where color/shading is proportional to ag_data$count) ?

Thank you!

4
  • 1
    You have read your shape file as an sp object. To make your life easier I would suggest to read it as an sf object using sf::st_read or convert my_shapefile to an sf object using sf::st_as_sf. An sf object behaves and looks more or less like a normal dataframe. After doing so you should be able to easily merge your aggregated data to the shape file by the regional identifier as outlined in the post you referenced.
    – stefan
    Commented Jul 23, 2022 at 8:37
  • @ stefan: thank you for this suggestion! I now used the following command to import the shapefile
    – stats_noob
    Commented Jul 23, 2022 at 17:38
  • shape_file<- sf::st_read("C:/Users/me/Documents/shape_folder/shape_file.shp", options = "ENCODING=WINDOWS-1252")
    – stats_noob
    Commented Jul 23, 2022 at 17:39
  • I will now start looking how to proceed from here!
    – stats_noob
    Commented Jul 23, 2022 at 17:39

0