1

Concerning my last post, I successfully plotted the school districts on a national map using the library tmap. Below is my attempt to plot the map.mymap ref As you can see, this is far behind the reference (by Laura Bliss: Bloomberg CityLab, 2015) that I want to follow. I guess there might be something wrong when I used st_make_valid(). Thus, I ran the following code to see the reasons behind Error: Shape contains invalid polygons (please note that I used Alabama (al) as an example, and the responses from st_is_valid(..., reason = TRUE) are 140 lines in total).

al <- map[map$STATEFP == "01", ]
st_is_valid(al, reason = TRUE)

[1] "Valid Geometry"                                         
[2] "Valid Geometry"                             
[3] "Loop 2: Edge 750 has duplicate vertex with edge 811"    
[4] "Loop 40: Edge 6246 has duplicate vertex with edge 6268" 
[5] "Loop 37: Edge 9 has duplicate vertex with edge 66"      
[6] "Loop 43: Edge 19 has duplicate vertex with edge 27"     
[7] "Loop 12: Edge 680 has duplicate vertex with edge 693"   
[8] "Loop 1: Edge 752 has duplicate vertex with edge 834"    
[9] "Loop 30: Edge 142 has duplicate vertex with edge 154"   
[10] "Valid Geometry"                                         
[11] "Valid Geometry"                                         
[12] "Loop 3: Edge 1619 has duplicate vertex with edge 1641"  
[13] "Valid Geometry"                                         
[14] "Loop 3: Edge 1533 has duplicate vertex with edge 1543"  
[15] "Valid Geometry"                                         

I am wondering if the dark areas on my map occurred because of st_make_valid(). If so, how should I fix this map to get a similar result as my reference? Any advice/suggestions/resources to solve this problem would be appreciated!

Kob
  • 147
  • 1
  • 11
  • I think the difference, bearing in mind that school districts are tiny, is whether you plot the district boundaries and fill, or just fill the polygons. On some level, only so much notation can be crammed into available space. Just filling polygons seems to be the route taken by your reference map maker. – Chris Apr 11 '22 at 21:36
  • Thanks for your comment @Chris. I only filled the polygons with one variable. This is a code that I used to generate this map. `tm_shape(test_plot) + tm_polygons("variable", id = "GEOID")`. To my understanding, this code only fills in the color of the district school level geometry (please feel free to correct me if I am wrong). Still, the problem existed, and I do not know why. – Kob Apr 11 '22 at 22:00

1 Answers1

3

As Chris pointed out, the darker areas are due to the density of the polygons you're mapping. You can adjust the border size with the lwd argument in tmap's functions.

Since the polygons are so dense, and you're filling them according to the data, you can probably go very low with the lwd argument. Default is 1.

Examples:

library(tmap)
library(sf)

nc <-  st_read(system.file("shape/nc.shp", package="sf"))

tm_shape(nc) + tm_polygons('BIR74',lwd = 0)

tm_shape(nc) + tm_polygons('BIR74',lwd = 5)

Created on 2022-04-11 by the reprex package (v2.0.1)

mrhellmann
  • 5,069
  • 11
  • 38
  • Thank you for your suggestion! @mrhellmann. You save me again--I appreciate it. Although I have not tried the nationwide map, I think this idea seems to work. May I ask for further clarification, please? 1. Do you, by any chance, know how to draw the border for the state and keep the color filled at district levels areas as shown in the reference? 2. Where can I read more about this? I searched google (of course) and found limited samples of school district maps. – Kob Apr 11 '22 at 22:42
  • In tmap, or perhaps I'm thinking mapview, you can have a basemap (your state boundaries) over which you plot your school districts, as was done in the reference map. – Chris Apr 11 '22 at 22:51
  • 1
    @Kob Get a shapefile of the states boundaries (maybe with the tigris pkg), and plot it along with your data. Should look something like this: `tm_shape(state_boundaries) + tm_polygon() + tm_shape(shcool_district_data) + tm_polygon(fill = 'column')` tmap can handle more than one shapefile at a time with multiple uses of `tm_shape`. you may need to make sure they use the same projection(crs). More about tmap:https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html The best book to start with mapping: https://geocompr.robinlovelace.net/ – mrhellmann Apr 12 '22 at 00:28
  • @Chris mapview does use a basemap for geographic data. Great package. – mrhellmann Apr 12 '22 at 00:30
  • 1
    @mrhellmann These suggestions are pretty helpful, and I appreciate it! It reduces a ton of research time. By the way, I am not sure how to check the projection (crs), but it seems to work when I plotted two maps together. Thank you! – Kob Apr 12 '22 at 22:50