2

I have an sf polygon object and am wondering how can I draw contour lines based on the column values. What I want to realize is something like below (this map is credited to here). enter image description here

My data look like below. Please download the data from here. I want to draw the contour lines based on column contour. My polygons are identical to the example above. Any thoughts?

Simple feature collection with 6 features and 4 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -88.47322 ymin: 30.22113 xmax: -84.89089 ymax: 35.00802
Geodetic CRS:  NAD83
  STATE STASD_N STASD_A                       geometry contour
1    01     110    0110 MULTIPOLYGON (((-87.92697 3...     141
2    01     120    0120 MULTIPOLYGON (((-85.59516 3...     190
3    01     130    0130 MULTIPOLYGON (((-87.91345 3...      76
4    01     140    0140 MULTIPOLYGON (((-87.83804 3...      70
5    01     150    0150 MULTIPOLYGON (((-88.08681 3...     112
6    01     160    0160 MULTIPOLYGON (((-84.99523 3...     167

Here are what I have tried with tmap. There is a tm_iso function however it only accepts spatial lines. Thus I converted my polygons to lines with st_cast. See the code below.

lines <- st_cast(asdsiso, 'MULTILINESTRING')

tm_shape(lines) + tm_iso(col = 'contour', text = 'contour')

enter image description here

Then I think probably I can aggregate the polygons. Thus I tried the code below. BUt the map is still the same except for the legend.

linesAgg <- linesAgg %>% group_by(contour2) %>%
  summarise(contour2 = mean(contour2))

tm_shape(linesAgg) + tm_iso(col = 'contour2', text = 'contour2')

The map is still the same except for the legend.

Yabin Da
  • 553
  • 5
  • 11
  • Asking us to download a 2+ MB file and do your project for you seems a bit much. You should take the time to show what coding efforts you have done so far. Surely you have done some data input and preliminary mapping? – IRTFM Aug 13 '21 at 00:43
  • 1
    And why not show what coding you have atempted using examples that are in the help pages of the functions for packages you have (well, not yet in your example anyway) loaded. – IRTFM Aug 13 '21 at 00:50
  • Perfect! I edited the post accordingly. I really appreciate your input! – Yabin Da Aug 13 '21 at 01:36
  • This site has some useful info. The added step in your case would be to convert the polygons to centroid points, then interpolate between those points. https://mgimond.github.io/Spatial/interpolation-in-r.html – nniloc Aug 13 '21 at 15:58
  • 1
    @nniloc You are right. I was able to draw those contour lines by following the website you suggested. It turned out that I have to interpolate the data to a raster (I first converted the polygons to points and interpolate over the points). Then I use the `st_contour` function to create lines from the raster. – Yabin Da Aug 29 '21 at 15:29
  • 1
    For anyone who is also looking for a solution to the problem, you can check this [example](https://www.yabin-da.com/notes_in_r/how-to-create-contour-lines-from-simple-features-of-an-sf-object/). – Yabin Da Aug 29 '21 at 22:29

0 Answers0