Spatial Data

Mapping in R
module 8
week 15
sf
shapefiles
ggplot2
maps
Author
Affiliation

School of Life Sciences, University of Hawaii

Published

May 1, 2025

Acknowledgements

References for this Material:

  • maps: https://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html

  • sf: https://r-spatial.org/r/2018/10/25/ggplot2-sf.html

Learning objectives

Learning objectives

At the end of this lesson you will:

  • Be familiar with spatial data in r, including using polygons and shapefiles
  • Understand how to subset and manipulate spatial data
  • Be able to create maps and data overlays in r
R packages you will need
  • maps
  • ggmap
  • sf
  • rnaturalearth
  • ggspatial
  • grid
  • scales
  • ggplot2
  • dplyr
  • magrittr

Overview

There are many great resources to plot maps in R. The first step is to download or access the map information, commonly stored as shape data (type .shp) The older maps package creates polygons from coordinate data, whereas the sf package is used to read and work with shape data.

maps

Download states data and explore. It is a pretty simple to get the map data. It is a dataframe with a series of latitude and longitude data for each state. Essentially itʻs the points that define the outline of the state if you connect the dots.

ℹ Google's Terms of Service: <https://mapsplatform.google.com>
  Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service>
  OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles>
ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
states <- map_data("state") # get map data from maps package
dim(states)
[1] 15537     6
head(states)
       long      lat group order  region subregion
1 -87.46201 30.38968     1     1 alabama      <NA>
2 -87.48493 30.37249     1     2 alabama      <NA>
3 -87.52503 30.37249     1     3 alabama      <NA>
4 -87.53076 30.33239     1     4 alabama      <NA>
5 -87.57087 30.32665     1     5 alabama      <NA>
6 -87.58806 30.32665     1     6 alabama      <NA>
tail(states)
           long      lat group order  region subregion
15594 -106.3295 41.00659    63 15594 wyoming      <NA>
15595 -106.8566 41.01232    63 15595 wyoming      <NA>
15596 -107.3093 41.01805    63 15596 wyoming      <NA>
15597 -107.9223 41.01805    63 15597 wyoming      <NA>
15598 -109.0568 40.98940    63 15598 wyoming      <NA>
15599 -109.0511 40.99513    63 15599 wyoming      <NA>

Plot the map data using geom_polygon and ggplot, plotting the latitude and longitude (Global Positioning System or GPS coordinates):

ggplot(data = states) + 
  geom_polygon(     # draw a polygon
    aes(x = long,   # use GPS coordinates
        y = lat, 
        fill = region,  # color by states 
        group = group), # group polygons by state
    color = "white") +  # borders are white
  coord_fixed(1.3) +    # aspect ratio
  guides(fill="none")  # do this to leave off the color legend

Since states it is just a dataframe, it is easy to subset. We can do it many ways, but here is an example using the subset function. This also demonstrates the point nature of the data and why specifying the group is important:

west_coast <- subset(states, region %in% c("california", "oregon", "washington", "hawaii"))

ggplot(data = west_coast) + 
  geom_polygon(
    aes(x = long,
        y = lat), 
    fill = "palegreen", 
    color = "black") 

Thatʻs better! In this dataset, group indicates the states by number. Now the points belong the states, and arenʻt plotted out of order.

ggplot(data = west_coast) + 
  geom_polygon(
    aes(x = long, 
        y = lat, 
        group = group), # group by state
    fill = "palegreen", 
    color = "black") + 
  coord_fixed(1.3)

An example zooming in on California

Get the geospatial data by subsetting. Get the county data too

ca_df <- subset(states, region == "california")
head(ca_df)
         long      lat group order     region subregion
667 -120.0060 42.00927     4   667 california      <NA>
668 -120.0060 41.20139     4   668 california      <NA>
669 -120.0060 39.70024     4   669 california      <NA>
670 -119.9946 39.44241     4   670 california      <NA>
671 -120.0060 39.31636     4   671 california      <NA>
672 -120.0060 39.16166     4   672 california      <NA>
counties <- map_data("county")
ca_county <- subset(counties, region == "california")
head(ca_county)
          long      lat group order     region subregion
6965 -121.4785 37.48290   157  6965 california   alameda
6966 -121.5129 37.48290   157  6966 california   alameda
6967 -121.8853 37.48290   157  6967 california   alameda
6968 -121.8968 37.46571   157  6968 california   alameda
6969 -121.9254 37.45998   157  6969 california   alameda
6970 -121.9483 37.47717   157  6970 california   alameda

Letʻs have a look at the county data. County info is in the subregion.

ca_base <- ggplot(
              data = ca_df, 
              mapping = aes(
                              x = long, 
                              y = lat, 
                              group = group)) + 
  coord_fixed(1.3) + 
  geom_polygon(color = "black", fill = "gray")

ca_base + theme_nothing()

ca_base + theme_nothing() + 
  geom_polygon(
      data = ca_county, # county outlines
      fill = NA, 
      color = "white") +
  geom_polygon(
      color = "black", 
      fill = NA)  # plot last so border on top layer

Try choosing a county and filling it with a different color.

sf package

The newer packages work with shapefiles, which are data types produced by ArgGIS and other spatial software. There is a lot of shapefile data available on the internet. For example, for Hawaii, see https://geoportal.hawaii.gov.

Load packages:

Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Attaching package: 'magrittr'
The following object is masked from 'package:ggmap':

    inset

rnaturalerth has a lot of map data already available, all you need to do is subset it to the region you wish to use.

sf is a package that reads, writes, and helps you work with shapefiles.

ggspacial has some nice add on functions

grid adds a rectangular grid to an existing plot

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
[1] "sf"         "data.frame"
class(world)
[1] "sf"         "data.frame"
print(world)
Simple feature collection with 242 features and 168 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961
Geodetic CRS:  WGS 84
First 10 features:
        featurecla scalerank labelrank                     sovereignt sov_a3
1  Admin-0 country         1         3                       Zimbabwe    ZWE
2  Admin-0 country         1         3                         Zambia    ZMB
3  Admin-0 country         1         3                          Yemen    YEM
4  Admin-0 country         3         2                        Vietnam    VNM
5  Admin-0 country         5         3                      Venezuela    VEN
6  Admin-0 country         6         6                        Vatican    VAT
7  Admin-0 country         1         4                        Vanuatu    VUT
8  Admin-0 country         1         3                     Uzbekistan    UZB
9  Admin-0 country         1         4                        Uruguay    URY
10 Admin-0 country         3         6 Federated States of Micronesia    FSM
   adm0_dif level              type tlc                          admin adm0_a3
1         0     2 Sovereign country   1                       Zimbabwe     ZWE
2         0     2 Sovereign country   1                         Zambia     ZMB
3         0     2 Sovereign country   1                          Yemen     YEM
4         0     2 Sovereign country   1                        Vietnam     VNM
5         0     2 Sovereign country   1                      Venezuela     VEN
6         0     2 Sovereign country   1                        Vatican     VAT
7         0     2 Sovereign country   1                        Vanuatu     VUT
8         0     2 Sovereign country   1                     Uzbekistan     UZB
9         0     2 Sovereign country   1                        Uruguay     URY
10        0     2 Sovereign country   1 Federated States of Micronesia     FSM
   geou_dif                        geounit gu_a3 su_dif
1         0                       Zimbabwe   ZWE      0
2         0                         Zambia   ZMB      0
3         0                          Yemen   YEM      0
4         0                        Vietnam   VNM      0
5         0                      Venezuela   VEN      0
6         0                        Vatican   VAT      0
7         0                        Vanuatu   VUT      0
8         0                     Uzbekistan   UZB      0
9         0                        Uruguay   URY      0
10        0 Federated States of Micronesia   FSM      0
                          subunit su_a3 brk_diff       name
1                        Zimbabwe   ZWE        0   Zimbabwe
2                          Zambia   ZMB        0     Zambia
3                           Yemen   YEM        0      Yemen
4                         Vietnam   VNM        0    Vietnam
5                       Venezuela   VEN        0  Venezuela
6                         Vatican   VAT        0    Vatican
7                         Vanuatu   VUT        0    Vanuatu
8                      Uzbekistan   UZB        0 Uzbekistan
9                         Uruguay   URY        0    Uruguay
10 Federated States of Micronesia   FSM        0 Micronesia
                        name_long brk_a3   brk_name brk_group abbrev postal
1                        Zimbabwe    ZWE   Zimbabwe      <NA>  Zimb.     ZW
2                          Zambia    ZMB     Zambia      <NA> Zambia     ZM
3                           Yemen    YEM      Yemen      <NA>   Yem.     YE
4                         Vietnam    VNM    Vietnam      <NA>  Viet.     VN
5                       Venezuela    VEN  Venezuela      <NA>   Ven.     VE
6                         Vatican    VAT    Vatican      <NA>   Vat.      V
7                         Vanuatu    VUT    Vanuatu      <NA>   Van.     VU
8                      Uzbekistan    UZB Uzbekistan      <NA>   Uzb.     UZ
9                         Uruguay    URY    Uruguay      <NA>   Ury.     UY
10 Federated States of Micronesia    FSM Micronesia      <NA> F.S.M.    FSM
                          formal_en                          formal_fr
1              Republic of Zimbabwe                               <NA>
2                Republic of Zambia                               <NA>
3                 Republic of Yemen                               <NA>
4     Socialist Republic of Vietnam                               <NA>
5  Bolivarian Republic of Venezuela República Bolivariana de Venezuela
6         State of the Vatican City                               <NA>
7               Republic of Vanuatu                               <NA>
8            Republic of Uzbekistan                               <NA>
9      Oriental Republic of Uruguay                               <NA>
10   Federated States of Micronesia                               <NA>
                        name_ciawf note_adm0 note_brk
1                         Zimbabwe      <NA>     <NA>
2                           Zambia      <NA>     <NA>
3                            Yemen      <NA>     <NA>
4                          Vietnam      <NA>     <NA>
5                        Venezuela      <NA>     <NA>
6          Holy See (Vatican City)      <NA>     <NA>
7                          Vanuatu      <NA>     <NA>
8                       Uzbekistan      <NA>     <NA>
9                          Uruguay      <NA>     <NA>
10 Micronesia, Federated States of      <NA>     <NA>
                         name_sort name_alt mapcolor7 mapcolor8 mapcolor9
1                         Zimbabwe     <NA>         1         5         3
2                           Zambia     <NA>         5         8         5
3                      Yemen, Rep.     <NA>         5         3         3
4                          Vietnam     <NA>         5         6         5
5                    Venezuela, RB     <NA>         1         3         1
6               Vatican (Holy See) Holy See         1         3         4
7                          Vanuatu     <NA>         6         3         7
8                       Uzbekistan     <NA>         2         3         5
9                          Uruguay     <NA>         1         2         2
10 Micronesia, Federated States of     <NA>         5         2         4
   mapcolor13  pop_est pop_rank pop_year gdp_md gdp_year
1           9 14645468       14     2019  21440     2019
2          13 17861030       14     2019  23309     2019
3          11 29161922       15     2019  22581     2019
4           4 96462106       16     2019 261921     2019
5           4 28515829       15     2019 482359     2014
6           2      825        2     2019    -99     2019
7           3   299882       10     2019    934     2019
8           4 33580650       15     2019  57921     2019
9          10  3461734       12     2019  56045     2019
10         13   113815        9     2019    401     2018
                      economy              income_grp fips_10 iso_a2 iso_a2_eh
1     5. Emerging region: G20           5. Low income      ZI     ZW        ZW
2   7. Least developed region  4. Lower middle income      ZA     ZM        ZM
3   7. Least developed region  4. Lower middle income      YM     YE        YE
4     5. Emerging region: G20  4. Lower middle income      VM     VN        VN
5     5. Emerging region: G20  3. Upper middle income      VE     VE        VE
6  2. Developed region: nonG7 2. High income: nonOECD      VT     VA        VA
7   7. Least developed region  4. Lower middle income      NH     VU        VU
8        6. Developing region  4. Lower middle income      UZ     UZ        UZ
9     5. Emerging region: G20  3. Upper middle income      UY     UY        UY
10       6. Developing region  4. Lower middle income      FM     FM        FM
   iso_a3 iso_a3_eh iso_n3 iso_n3_eh un_a3 wb_a2 wb_a3   woe_id woe_id_eh
1     ZWE       ZWE    716       716   716    ZW   ZWE 23425004  23425004
2     ZMB       ZMB    894       894   894    ZM   ZMB 23425003  23425003
3     YEM       YEM    887       887   887    RY   YEM 23425002  23425002
4     VNM       VNM    704       704   704    VN   VNM 23424984  23424984
5     VEN       VEN    862       862   862    VE   VEN 23424982  23424982
6     VAT       VAT    336       336   336   -99   -99 23424986  23424986
7     VUT       VUT    548       548   548    VU   VUT 23424907  23424907
8     UZB       UZB    860       860   860    UZ   UZB 23424980  23424980
9     URY       URY    858       858   858    UY   URY 23424979  23424979
10    FSM       FSM    583       583   583    FM   FSM 23424815  23424815
                     woe_note adm0_iso adm0_diff adm0_tlc adm0_a3_us adm0_a3_fr
1  Exact WOE match as country      ZWE      <NA>      ZWE        ZWE        ZWE
2  Exact WOE match as country      ZMB      <NA>      ZMB        ZMB        ZMB
3  Exact WOE match as country      YEM      <NA>      YEM        YEM        YEM
4  Exact WOE match as country      VNM      <NA>      VNM        VNM        VNM
5  Exact WOE match as country      VEN      <NA>      VEN        VEN        VEN
6  Exact WOE match as country      VAT      <NA>      VAT        VAT        VAT
7  Exact WOE match as country      VUT      <NA>      VUT        VUT        VUT
8  Exact WOE match as country      UZB      <NA>      UZB        UZB        UZB
9  Exact WOE match as country      URY      <NA>      URY        URY        URY
10 Exact WOE match as country      FSM      <NA>      FSM        FSM        FSM
   adm0_a3_ru adm0_a3_es adm0_a3_cn adm0_a3_tw adm0_a3_in adm0_a3_np adm0_a3_pk
1         ZWE        ZWE        ZWE        ZWE        ZWE        ZWE        ZWE
2         ZMB        ZMB        ZMB        ZMB        ZMB        ZMB        ZMB
3         YEM        YEM        YEM        YEM        YEM        YEM        YEM
4         VNM        VNM        VNM        VNM        VNM        VNM        VNM
5         VEN        VEN        VEN        VEN        VEN        VEN        VEN
6         VAT        VAT        VAT        VAT        VAT        VAT        VAT
7         VUT        VUT        VUT        VUT        VUT        VUT        VUT
8         UZB        UZB        UZB        UZB        UZB        UZB        UZB
9         URY        URY        URY        URY        URY        URY        URY
10        FSM        FSM        FSM        FSM        FSM        FSM        FSM
   adm0_a3_de adm0_a3_gb adm0_a3_br adm0_a3_il adm0_a3_ps adm0_a3_sa adm0_a3_eg
1         ZWE        ZWE        ZWE        ZWE        ZWE        ZWE        ZWE
2         ZMB        ZMB        ZMB        ZMB        ZMB        ZMB        ZMB
3         YEM        YEM        YEM        YEM        YEM        YEM        YEM
4         VNM        VNM        VNM        VNM        VNM        VNM        VNM
5         VEN        VEN        VEN        VEN        VEN        VEN        VEN
6         VAT        VAT        VAT        VAT        VAT        VAT        VAT
7         VUT        VUT        VUT        VUT        VUT        VUT        VUT
8         UZB        UZB        UZB        UZB        UZB        UZB        UZB
9         URY        URY        URY        URY        URY        URY        URY
10        FSM        FSM        FSM        FSM        FSM        FSM        FSM
   adm0_a3_ma adm0_a3_pt adm0_a3_ar adm0_a3_jp adm0_a3_ko adm0_a3_vn adm0_a3_tr
1         ZWE        ZWE        ZWE        ZWE        ZWE        ZWE        ZWE
2         ZMB        ZMB        ZMB        ZMB        ZMB        ZMB        ZMB
3         YEM        YEM        YEM        YEM        YEM        YEM        YEM
4         VNM        VNM        VNM        VNM        VNM        VNM        VNM
5         VEN        VEN        VEN        VEN        VEN        VEN        VEN
6         VAT        VAT        VAT        VAT        VAT        VAT        VAT
7         VUT        VUT        VUT        VUT        VUT        VUT        VUT
8         UZB        UZB        UZB        UZB        UZB        UZB        UZB
9         URY        URY        URY        URY        URY        URY        URY
10        FSM        FSM        FSM        FSM        FSM        FSM        FSM
   adm0_a3_id adm0_a3_pl adm0_a3_gr adm0_a3_it adm0_a3_nl adm0_a3_se adm0_a3_bd
1         ZWE        ZWE        ZWE        ZWE        ZWE        ZWE        ZWE
2         ZMB        ZMB        ZMB        ZMB        ZMB        ZMB        ZMB
3         YEM        YEM        YEM        YEM        YEM        YEM        YEM
4         VNM        VNM        VNM        VNM        VNM        VNM        VNM
5         VEN        VEN        VEN        VEN        VEN        VEN        VEN
6         VAT        VAT        VAT        VAT        VAT        VAT        VAT
7         VUT        VUT        VUT        VUT        VUT        VUT        VUT
8         UZB        UZB        UZB        UZB        UZB        UZB        UZB
9         URY        URY        URY        URY        URY        URY        URY
10        FSM        FSM        FSM        FSM        FSM        FSM        FSM
   adm0_a3_ua adm0_a3_un adm0_a3_wb     continent region_un          subregion
1         ZWE        -99        -99        Africa    Africa     Eastern Africa
2         ZMB        -99        -99        Africa    Africa     Eastern Africa
3         YEM        -99        -99          Asia      Asia       Western Asia
4         VNM        -99        -99          Asia      Asia South-Eastern Asia
5         VEN        -99        -99 South America  Americas      South America
6         VAT        -99        -99        Europe    Europe    Southern Europe
7         VUT        -99        -99       Oceania   Oceania          Melanesia
8         UZB        -99        -99          Asia      Asia       Central Asia
9         URY        -99        -99 South America  Americas      South America
10        FSM        -99        -99       Oceania   Oceania         Micronesia
                    region_wb name_len long_len abbrev_len tiny homepart
1          Sub-Saharan Africa        8        8          5  -99        1
2          Sub-Saharan Africa        6        6          6  -99        1
3  Middle East & North Africa        5        5          4  -99        1
4         East Asia & Pacific        7        7          5    2        1
5   Latin America & Caribbean        9        9          4  -99        1
6       Europe & Central Asia        7        7          4    4        1
7         East Asia & Pacific        7        7          4    2        1
8       Europe & Central Asia       10       10          4    5        1
9   Latin America & Caribbean        7        7          4  -99        1
10        East Asia & Pacific       10       30          6  -99        1
   min_zoom min_label max_label   label_x    label_y      ne_id wikidataid
1         0       2.5       8.0  29.92544 -18.911640 1159321441       Q954
2         0       3.0       8.0  26.39530 -14.660804 1159321439       Q953
3         0       3.0       8.0  45.87438  15.328226 1159321425       Q805
4         0       2.0       7.0 105.38729  21.715416 1159321417       Q881
5         0       2.5       7.5 -64.59938   7.182476 1159321411       Q717
6         0       5.0      10.0  12.45342  41.903323 1159321407       Q237
7         0       4.0       9.0 166.90876 -15.371530 1159321421       Q686
8         0       3.0       8.0  64.00543  41.693603 1159321405       Q265
9         0       3.0       8.0 -55.96694 -32.961127 1159321353        Q77
10        0       5.0      10.0 158.23402   6.887553 1159320691       Q702
                     name_ar              name_bn
1                   زيمبابوي             জিম্বাবুয়ে
2                     زامبيا              জাম্বিয়া
3                      اليمن               ইয়েমেন
4                     فيتنام             ভিয়েতনাম
5                    فنزويلا            ভেনেজুয়েলা
6                  الفاتيكان        ভ্যাটিকান সিটি
7                    فانواتو               ভানুয়াতু
8                  أوزبكستان           উজবেকিস্তান
9                 الأوروغواي                উরুগুয়ে
10 ولايات ميكرونيسيا المتحدة মাইক্রোনেশিয়া যুক্তরাজ্য
                              name_de                        name_en
1                            Simbabwe                       Zimbabwe
2                              Sambia                         Zambia
3                               Jemen                          Yemen
4                             Vietnam                        Vietnam
5                           Venezuela                      Venezuela
6                        Vatikanstadt                   Vatican City
7                             Vanuatu                        Vanuatu
8                          Usbekistan                     Uzbekistan
9                             Uruguay                        Uruguay
10 Föderierte Staaten von Mikronesien Federated States of Micronesia
                           name_es  name_fa                     name_fr
1                         Zimbabue زیمبابوه                    Zimbabwe
2                           Zambia   زامبیا                      Zambie
3                            Yemen      یمن                       Yémen
4                          Vietnam   ویتنام                    Viêt Nam
5                        Venezuela  ونزوئلا                   Venezuela
6              Ciudad del Vaticano  واتیکان             Cité du Vatican
7                          Vanuatu  وانواتو                     Vanuatu
8                       Uzbekistán ازبکستان                 Ouzbékistan
9                          Uruguay  اروگوئه                     Uruguay
10 Estados Federados de Micronesia میکرونزی États fédérés de Micronésie
                                name_el       name_he                  name_hi
1                            Ζιμπάμπουε      זימבבואה                  ज़िम्बाब्वे
2                                Ζάμπια         זמביה                  ज़ाम्बिया
3                                Υεμένη          תימן                      यमन
4                               Βιετνάμ       וייטנאם                  वियतनाम
5                            Βενεζουέλα       ונצואלה                   वेनेज़ुएला
6                              Βατικανό קריית הוותיקן                वैटिकन नगर
7                             Βανουάτου        ונואטו                    वानूआटू
8                          Ουζμπεκιστάν     אוזבקיסטן                उज़्बेकिस्तान
9                            Ουρουγουάη     אורוגוואי                     उरुग्वे
10 Ομόσπονδες Πολιτείες της Μικρονησίας     מיקרונזיה माइक्रोनेशिया के संघीकृत राज्य
                          name_hu    name_id                      name_it
1                        Zimbabwe   Zimbabwe                     Zimbabwe
2                          Zambia     Zambia                       Zambia
3                           Jemen      Yaman                        Yemen
4                         Vietnám    Vietnam                      Vietnam
5                       Venezuela  Venezuela                    Venezuela
6                         Vatikán    Vatikan           Città del Vaticano
7                         Vanuatu    Vanuatu                      Vanuatu
8                     Üzbegisztán Uzbekistan                   Uzbekistan
9                         Uruguay    Uruguay                      Uruguay
10 Mikronéziai Szövetségi Államok Mikronesia Stati Federati di Micronesia
            name_ja           name_ko      name_nl    name_pl     name_pt
1        ジンバブエ          짐바브웨     Zimbabwe   Zimbabwe    Zimbábue
2          ザンビア            잠비아       Zambia     Zambia      Zâmbia
3          イエメン              예멘        Jemen      Jemen       Iémen
4          ベトナム            베트남      Vietnam    Wietnam    Vietname
5        ベネズエラ        베네수엘라    Venezuela  Wenezuela   Venezuela
6          バチカン       바티칸 시국 Vaticaanstad    Watykan    Vaticano
7          バヌアツ          바누아투      Vanuatu    Vanuatu     Vanuatu
8    ウズベキスタン      우즈베키스탄  Oezbekistan Uzbekistan Uzbequistão
9        ウルグアイ          우루과이      Uruguay    Urugwaj     Uruguai
10 ミクロネシア連邦 미크로네시아 연방   Micronesia Mikronezja  Micronésia
      name_ru                   name_sv    name_tr    name_uk      name_ur
1    Зимбабве                  Zimbabwe   Zimbabve   Зімбабве      زمبابوے
2      Замбия                    Zambia    Zambiya     Замбія       زیمبیا
3       Йемен                     Jemen      Yemen       Ємен          یمن
4     Вьетнам                   Vietnam    Vietnam    В'єтнам       ویتنام
5   Венесуэла                 Venezuela  Venezuela  Венесуела    وینیزویلا
6     Ватикан             Vatikanstaten    Vatikan    Ватикан   ویٹیکن سٹی
7     Вануату                   Vanuatu    Vanuatu    Вануату      وانواتو
8  Узбекистан                Uzbekistan Özbekistan Узбекистан     ازبکستان
9     Уругвай                   Uruguay    Uruguay    Уругвай     یوراگوئے
10 Микронезия Mikronesiska federationen Mikronezya Мікронезія مائیکرونیشیا
         name_vi          name_zh         name_zht      fclass_iso tlc_diff
1       Zimbabwe         津巴布韦           辛巴威 Admin-0 country     <NA>
2         Zambia           赞比亚           尚比亞 Admin-0 country     <NA>
3          Yemen             也门             葉門 Admin-0 country     <NA>
4       Việt Nam             越南             越南 Admin-0 country     <NA>
5      Venezuela         委内瑞拉         委內瑞拉 Admin-0 country     <NA>
6  Thành Vatican           梵蒂冈           梵蒂岡 Admin-0 country     <NA>
7        Vanuatu         瓦努阿图           萬那杜 Admin-0 country     <NA>
8     Uzbekistan     乌兹别克斯坦         烏茲別克 Admin-0 country     <NA>
9        Uruguay           乌拉圭           烏拉圭 Admin-0 country     <NA>
10    Micronesia 密克罗尼西亚联邦 密克羅尼西亞聯邦 Admin-0 country     <NA>
        fclass_tlc fclass_us fclass_fr fclass_ru fclass_es fclass_cn fclass_tw
1  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
2  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
3  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
4  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
5  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
6  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
7  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
8  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
9  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
10 Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
   fclass_in fclass_np fclass_pk fclass_de fclass_gb fclass_br fclass_il
1       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
2       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
3       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
4       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
5       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
6       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
7       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
8       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
9       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
10      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
   fclass_ps fclass_sa fclass_eg fclass_ma fclass_pt fclass_ar fclass_jp
1       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
2       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
3       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
4       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
5       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
6       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
7       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
8       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
9       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
10      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
   fclass_ko fclass_vn fclass_tr fclass_id fclass_pl fclass_gr fclass_it
1       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
2       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
3       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
4       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
5       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
6       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
7       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
8       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
9       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
10      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
   fclass_nl fclass_se fclass_bd fclass_ua                       geometry
1       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((31.28789 -2...
2       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((30.39609 -1...
3       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((53.08564 16...
4       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((104.064 10....
5       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((-60.82119 9...
6       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((12.43916 41...
7       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((166.7458 -1...
8       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((70.94678 42...
9       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((-53.37061 -...
10      <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((162.9832 5....
names(world)
  [1] "featurecla" "scalerank"  "labelrank"  "sovereignt" "sov_a3"    
  [6] "adm0_dif"   "level"      "type"       "tlc"        "admin"     
 [11] "adm0_a3"    "geou_dif"   "geounit"    "gu_a3"      "su_dif"    
 [16] "subunit"    "su_a3"      "brk_diff"   "name"       "name_long" 
 [21] "brk_a3"     "brk_name"   "brk_group"  "abbrev"     "postal"    
 [26] "formal_en"  "formal_fr"  "name_ciawf" "note_adm0"  "note_brk"  
 [31] "name_sort"  "name_alt"   "mapcolor7"  "mapcolor8"  "mapcolor9" 
 [36] "mapcolor13" "pop_est"    "pop_rank"   "pop_year"   "gdp_md"    
 [41] "gdp_year"   "economy"    "income_grp" "fips_10"    "iso_a2"    
 [46] "iso_a2_eh"  "iso_a3"     "iso_a3_eh"  "iso_n3"     "iso_n3_eh" 
 [51] "un_a3"      "wb_a2"      "wb_a3"      "woe_id"     "woe_id_eh" 
 [56] "woe_note"   "adm0_iso"   "adm0_diff"  "adm0_tlc"   "adm0_a3_us"
 [61] "adm0_a3_fr" "adm0_a3_ru" "adm0_a3_es" "adm0_a3_cn" "adm0_a3_tw"
 [66] "adm0_a3_in" "adm0_a3_np" "adm0_a3_pk" "adm0_a3_de" "adm0_a3_gb"
 [71] "adm0_a3_br" "adm0_a3_il" "adm0_a3_ps" "adm0_a3_sa" "adm0_a3_eg"
 [76] "adm0_a3_ma" "adm0_a3_pt" "adm0_a3_ar" "adm0_a3_jp" "adm0_a3_ko"
 [81] "adm0_a3_vn" "adm0_a3_tr" "adm0_a3_id" "adm0_a3_pl" "adm0_a3_gr"
 [86] "adm0_a3_it" "adm0_a3_nl" "adm0_a3_se" "adm0_a3_bd" "adm0_a3_ua"
 [91] "adm0_a3_un" "adm0_a3_wb" "continent"  "region_un"  "subregion" 
 [96] "region_wb"  "name_len"   "long_len"   "abbrev_len" "tiny"      
[101] "homepart"   "min_zoom"   "min_label"  "max_label"  "label_x"   
[106] "label_y"    "ne_id"      "wikidataid" "name_ar"    "name_bn"   
[111] "name_de"    "name_en"    "name_es"    "name_fa"    "name_fr"   
[116] "name_el"    "name_he"    "name_hi"    "name_hu"    "name_id"   
[121] "name_it"    "name_ja"    "name_ko"    "name_nl"    "name_pl"   
[126] "name_pt"    "name_ru"    "name_sv"    "name_tr"    "name_uk"   
[131] "name_ur"    "name_vi"    "name_zh"    "name_zht"   "fclass_iso"
[136] "tlc_diff"   "fclass_tlc" "fclass_us"  "fclass_fr"  "fclass_ru" 
[141] "fclass_es"  "fclass_cn"  "fclass_tw"  "fclass_in"  "fclass_np" 
[146] "fclass_pk"  "fclass_de"  "fclass_gb"  "fclass_br"  "fclass_il" 
[151] "fclass_ps"  "fclass_sa"  "fclass_eg"  "fclass_ma"  "fclass_pt" 
[156] "fclass_ar"  "fclass_jp"  "fclass_ko"  "fclass_vn"  "fclass_tr" 
[161] "fclass_id"  "fclass_pl"  "fclass_gr"  "fclass_it"  "fclass_nl" 
[166] "fclass_se"  "fclass_bd"  "fclass_ua"  "geometry"  
world_map <- world %>% ggplot() + 
      geom_sf()

world_map + 
  xlab("Longitude") + ylab("Latitude") +
    ggtitle("World map", subtitle = paste0("(", length(unique(world$name)), " countries)"))

world_map + 
  xlab("Longitude") + ylab("Latitude") +
    ggtitle("World map", subtitle = paste0("(", length(unique(world$name)), " countries)")) + 
    geom_sf(aes(fill = pop_est), show.legend="point") +
    scale_fill_viridis_c(option = "plasma", trans = "sqrt")

world %>% ggplot() + 
      geom_sf(color = "black", fill = "lightgreen")

usa <- ne_states(country="United States of America", returnclass = "sf")
head(usa)
Simple feature collection with 6 features and 121 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -124.7346 ymin: 41.69681 xmax: -82.4146 ymax: 49.36949
Geodetic CRS:  WGS 84
                         featurecla scalerank adm1_code diss_me iso_3166_2
1236 Admin-1 states provinces lakes         2  USA-3519    3519      US-WA
1238 Admin-1 states provinces lakes         2  USA-3518    3518      US-ID
1239 Admin-1 states provinces lakes         2  USA-3515    3515      US-MT
1242 Admin-1 states provinces lakes         2  USA-3516    3516      US-ND
1244 Admin-1 states provinces lakes         2  USA-3514    3514      US-MN
1246 Admin-1 states provinces lakes         2  USA-3562    3562      US-MI
                                           wikipedia iso_a2 adm0_sr
1236 http://en.wikipedia.org/wiki/Washington_(state)     US       6
1238              http://en.wikipedia.org/wiki/Idaho     US       1
1239            http://en.wikipedia.org/wiki/Montana     US       1
1242       http://en.wikipedia.org/wiki/North_Dakota     US       1
1244          http://en.wikipedia.org/wiki/Minnesota     US       1
1246           http://en.wikipedia.org/wiki/Michigan     US       1
             name name_alt name_local  type type_en code_local code_hasc note
1236   Washington WA|Wash.       <NA> State   State       US53     US.WA <NA>
1238        Idaho ID|Idaho       <NA> State   State       US16     US.ID <NA>
1239      Montana MT|Mont.       <NA> State   State       US30     US.MT <NA>
1242 North Dakota  ND|N.D.       <NA> State   State       US38     US.ND <NA>
1244    Minnesota MN|Minn.       <NA> State   State       US27     US.MN <NA>
1246     Michigan MI|Mich.       <NA> State   State       US26     US.MI <NA>
     hasc_maybe  region region_cod provnum_ne gadm_level check_me datarank
1236       <NA>    West       <NA>          0          1       20        1
1238       <NA>    West       <NA>          0          1       20        1
1239       <NA>    West       <NA>          0          1       20        1
1242       <NA> Midwest       <NA>          0          1       20        1
1244       <NA> Midwest       <NA>          0          1       20        1
1246       <NA> Midwest       <NA>          0          1       20        1
     abbrev postal area_sqkm sameascity labelrank name_len mapcolor9 mapcolor13
1236  Wash.     WA         0        -99         0       10         1          1
1238  Idaho     ID         0        -99         0        5         1          1
1239  Mont.     MT         0        -99         0        7         1          1
1242   N.D.     ND         0        -99         0       12         1          1
1244  Minn.     MN         0        -99         0        9         1          1
1246  Mich.     MI         0        -99         0        8         1          1
     fips fips_alt  woe_id                       woe_label     woe_name
1236 US53     <NA> 2347606   Washington, US, United States   Washington
1238 US16     <NA> 2347571        Idaho, US, United States        Idaho
1239 US30     <NA> 2347585      Montana, US, United States      Montana
1242 US38     <NA> 2347593 North Dakota, US, United States North Dakota
1244 US27     <NA> 2347582    Minnesota, US, United States    Minnesota
1246 US26     <NA> 2347581     Michigan, US, United States     Michigan
     latitude longitude sov_a3 adm0_a3 adm0_label                    admin
1236  47.4865 -120.3610    US1     USA          2 United States of America
1238  43.7825 -114.1330    US1     USA          2 United States of America
1239  46.9965 -110.0440    US1     USA          2 United States of America
1242  47.4675 -100.3020    US1     USA          2 United States of America
1244  46.0592  -93.3640    US1     USA          2 United States of America
1246  43.4343  -84.9479    US1     USA          2 United States of America
                     geonunit gu_a3   gn_id      gn_name gns_id gns_name
1236 United States of America   USA 5815135   Washington     -1     <NA>
1238 United States of America   USA 5596512        Idaho     -1     <NA>
1239 United States of America   USA 5667009      Montana     -1     <NA>
1242 United States of America   USA 5690763 North Dakota     -1     <NA>
1244 United States of America   USA 5037779    Minnesota     -1     <NA>
1246 United States of America   USA 5001836     Michigan     -1     <NA>
     gn_level gn_region gn_a1_code         region_sub sub_code gns_level
1236        1      <NA>      US.WA            Pacific     <NA>        -1
1238        1      <NA>      US.ID           Mountain     <NA>        -1
1239        1      <NA>      US.MT           Mountain     <NA>        -1
1242        1      <NA>      US.ND West North Central     <NA>        -1
1244        1      <NA>      US.MN West North Central     <NA>        -1
1246        1      <NA>      US.MI East North Central     <NA>        -1
     gns_lang gns_adm1 gns_region min_label max_label min_zoom wikidataid
1236     <NA>     <NA>       <NA>       3.5       7.5        2      Q1223
1238     <NA>     <NA>       <NA>       3.5       7.5        2      Q1221
1239     <NA>     <NA>       <NA>       3.5       7.5        2      Q1212
1242     <NA>     <NA>       <NA>       3.5       7.5        2      Q1207
1244     <NA>     <NA>       <NA>       3.5       7.5        2      Q1527
1246     <NA>     <NA>       <NA>       3.5       7.5        2      Q1166
             name_ar    name_bn      name_de      name_en          name_es
1236          واشنطن   ওয়াশিংটন   Washington   Washington       Washington
1238          أيداهو     আইডাহো        Idaho        Idaho            Idaho
1239         مونتانا     মন্টানা      Montana      Montana          Montana
1242 داكوتا الشمالية নর্থ ডাকোটা North Dakota North Dakota Dakota del Norte
1244        مينيسوتا   মিনেসোটা    Minnesota    Minnesota        Minnesota
1246         ميشيغان    মিশিগান     Michigan     Michigan         Míchigan
            name_fr        name_el      name_hi      name_hu      name_id
1236     Washington    Ουάσινγκτον वॉशिंगटन राज्य   Washington   Washington
1238          Idaho        Άινταχο       आयडाहो        Idaho        Idaho
1239        Montana        Μοντάνα      मोन्टाना      Montana      Montana
1242 Dakota du Nord Βόρεια Ντακότα   उत्तर डेकोटा Észak-Dakota Dakota Utara
1244      Minnesota       Μινεσότα      मिनेसोटा    Minnesota    Minnesota
1246       Michigan       Μίσιγκαν       मिशिगन     Michigan     Michigan
             name_it        name_ja    name_ko      name_nl         name_pl
1236      Washington   ワシントン州     워싱턴   Washington      Waszyngton
1238           Idaho     アイダホ州   아이다호        Idaho           Idaho
1239         Montana     モンタナ州     몬태나      Montana         Montana
1242 Dakota del Nord ノースダコタ州 노스다코타 Noord-Dakota Dakota Północna
1244       Minnesota     ミネソタ州   미네소타    Minnesota       Minnesota
1246        Michigan     ミシガン州     미시간     Michigan        Michigan
             name_pt         name_ru      name_sv      name_tr    name_vi
1236      Washington       Вашингтон   Washington    Vaşington Washington
1238           Idaho          Айдахо        Idaho        Idaho      Idaho
1239         Montana         Монтана      Montana      Montana    Montana
1242 Dakota do Norte Северная Дакота North Dakota Kuzey Dakota Bắc Dakota
1244       Minnesota       Миннесота    Minnesota    Minnesota  Minnesota
1246        Michigan         Мичиган     Michigan     Michigan   Michigan
        name_zh      ne_id       name_he         name_uk       name_ur
1236   华盛顿州 1159309547     וושינגטון       Вашингтон ریاست واشنگٹن
1238   爱达荷州 1159315339        איידהו          Айдахо        ایڈاہو
1239   蒙大拿州 1159315333        מונטנה         Монтана       مونٹانا
1242 北达科他州 1159315337 דקוטה הצפונית Північна Дакота   شمالی ڈکوٹا
1244 明尼苏达州 1159315297       מינסוטה       Міннесота      مینیسوٹا
1246   密歇根州 1159314665        מישיגן         Мічиган        مشی گن
            name_fa   name_zht FCLASS_ISO FCLASS_US FCLASS_FR FCLASS_RU
1236 ایالت واشینگتن   華盛頓州       <NA>      <NA>      <NA>      <NA>
1238         آیداهو   愛達荷州       <NA>      <NA>      <NA>      <NA>
1239  ایالت مونتانا   蒙大拿州       <NA>      <NA>      <NA>      <NA>
1242  داکوتای شمالی 北達科他州       <NA>      <NA>      <NA>      <NA>
1244       مینهسوتا 明尼蘇達州       <NA>      <NA>      <NA>      <NA>
1246        میشیگان   密歇根州       <NA>      <NA>      <NA>      <NA>
     FCLASS_ES FCLASS_CN FCLASS_TW FCLASS_IN FCLASS_NP FCLASS_PK FCLASS_DE
1236      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1238      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1239      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1242      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1244      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1246      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
     FCLASS_GB FCLASS_BR FCLASS_IL FCLASS_PS FCLASS_SA FCLASS_EG FCLASS_MA
1236      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1238      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1239      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1242      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1244      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1246      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
     FCLASS_PT FCLASS_AR FCLASS_JP FCLASS_KO FCLASS_VN FCLASS_TR FCLASS_ID
1236      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1238      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1239      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1242      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1244      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1246      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
     FCLASS_PL FCLASS_GR FCLASS_IT FCLASS_NL FCLASS_SE FCLASS_BD FCLASS_UA
1236      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1238      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1239      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1242      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1244      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
1246      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
     FCLASS_TLC                       geometry
1236       <NA> MULTIPOLYGON (((-122.753 48...
1238       <NA> MULTIPOLYGON (((-117.0382 4...
1239       <NA> MULTIPOLYGON (((-116.0482 4...
1242       <NA> MULTIPOLYGON (((-104.0476 4...
1244       <NA> MULTIPOLYGON (((-97.22609 4...
1246       <NA> MULTIPOLYGON (((-84.4913 46...
hi <- usa %>% 
    filter(name %in% "Hawaii")

ggplot(hi) + geom_sf()

ggplot(data=usa) + 
  geom_sf()

plants_sf <- read_sf("Threatened-Endangered_Plants/Threatened-Endangered_Plants.shp")

print(plants_sf)
Simple feature collection with 295 features and 4 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 418663.1 ymin: 2094235 xmax: 940279.3 ymax: 2458614
Projected CRS: NAD83(HARN) / UTM zone 4N
# A tibble: 295 × 5
   objectid density st_areasha st_perimet                               geometry
      <int> <chr>        <dbl>      <dbl>                          <POLYGON [m]>
 1        1 O       486756841.    529043. ((458315.8 2458250, 458323.8 2458261,…
 2        2 O         1902211.     10318. ((442761.6 2457820, 442825.4 2457776,…
 3        3 VH       91796978.     55012. ((439743.9 2457337, 439749.5 2457315,…
 4        4 M       327957790.    178672. ((442266 2457081, 442399.4 2457054, 4…
 5        5 O         3303196.     21531. ((445019.8 2456927, 445048.5 2456954,…
 6        6 L         5716987.     19788. ((454844.9 2452549, 454657 2452625, 4…
 7        7 O           93867.      1576. ((435817.1 2454756, 435799.7 2454719,…
 8        8 L         1422708.      6576. ((457829.4 2452003, 457570.8 2451935,…
 9        9 O         1274152.      5524. ((433566.3 2452939, 433550 2452773, 4…
10       10 M        21326155.     56140. ((425412.9 2449482, 425419.3 2449480,…
# ℹ 285 more rows
st_geometry(plants_sf)
Geometry set for 295 features 
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 418663.1 ymin: 2094235 xmax: 940279.3 ymax: 2458614
Projected CRS: NAD83(HARN) / UTM zone 4N
First 5 geometries:
POLYGON ((458315.8 2458250, 458323.8 2458261, 4...
POLYGON ((442761.6 2457820, 442825.4 2457776, 4...
POLYGON ((439743.9 2457337, 439749.5 2457315, 4...
POLYGON ((442266 2457081, 442399.4 2457054, 442...
POLYGON ((445019.8 2456927, 445048.5 2456954, 4...
old_sf <- read_sf("USGS_Quads_(Old_Hawaiian_Datum)/USGS_Quads_(Old_Hawaiian_Datum).shp")

print(old_sf)
Simple feature collection with 128 features and 5 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 367593.4 ymin: 2089668 xmax: 946763.1 ymax: 2459366
Projected CRS: NAD83(HARN) / UTM zone 4N
# A tibble: 128 × 6
   objectid qname         qnum  st_areasha st_perimet                   geometry
      <int> <chr>         <chr>      <dbl>      <dbl>              <POLYGON [m]>
 1        1 HAENA         K03   178152007.     53420. ((431559.5 2452918, 43159…
 2        2 HANALEI       K06   178109861.     53416. ((444471.9 2459311, 45735…
 3        3 ANAHOLA       K09   178076759.     53411. ((457315.4 2445444, 45735…
 4        4 MAKAHA POINT  K01   178290448.     53441. ((418674 2452974, 431559.…
 5        5 WAIMEA CANYON K04   178470118.     53467. ((431529.2 2445543, 44442…
 6        6 WAIALEALE     K07   178392925.     53458. ((444422.5 2445489, 45731…
 7        7 KAPAA         K10   178336247.     53449. ((457315.4 2445444, 47020…
 8        8 KEKAHA        K02   178551324.     53480. ((418602.1 2439147, 43149…
 9        9 NIIHAU NORTH  N01   380920692.     81144. ((367716.3 2421892, 36783…
10       10 HANAPEPE      K05   178659961.     53495. ((431468.3 2431707, 44437…
# ℹ 118 more rows
plot(old_sf)

NG map

ng_sf <- ne_countries(country = 
            c(
              "Papua New Guinea", 
              "Indonesia", 
              "Philippines"
            ), 
            scale=50, 
            returnclass="sf")
print(ng_sf)
Simple feature collection with 3 features and 168 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 95.20664 ymin: -11.63057 xmax: 155.9576 ymax: 20.84126
Geodetic CRS:  WGS 84
         featurecla scalerank labelrank       sovereignt sov_a3 adm0_dif level
81  Admin-0 country         1         2      Philippines    PHL        0     2
84  Admin-0 country         1         2 Papua New Guinea    PNG        0     2
144 Admin-0 country         3         2        Indonesia    IDN        0     2
                 type tlc            admin adm0_a3 geou_dif          geounit
81  Sovereign country   1      Philippines     PHL        0      Philippines
84  Sovereign country   1 Papua New Guinea     PNG        0 Papua New Guinea
144 Sovereign country   1        Indonesia     IDN        0        Indonesia
    gu_a3 su_dif          subunit su_a3 brk_diff             name
81    PHL      0      Philippines   PHL        0      Philippines
84    PNG      1 Papua New Guinea   PN1        0 Papua New Guinea
144   IDN      0        Indonesia   IDN        0        Indonesia
           name_long brk_a3         brk_name brk_group abbrev postal
81       Philippines    PHL      Philippines      <NA>  Phil.     PH
84  Papua New Guinea    PN1 Papua New Guinea      <NA> P.N.G.     PG
144        Indonesia    IDN        Indonesia      <NA>  Indo.   INDO
                                formal_en formal_fr       name_ciawf note_adm0
81            Republic of the Philippines      <NA>      Philippines      <NA>
84  Independent State of Papua New Guinea      <NA> Papua New Guinea      <NA>
144                 Republic of Indonesia      <NA>        Indonesia      <NA>
    note_brk        name_sort name_alt mapcolor7 mapcolor8 mapcolor9 mapcolor13
81      <NA>      Philippines     <NA>         3         2         2          8
84      <NA> Papua New Guinea     <NA>         4         2         3          1
144     <NA>        Indonesia     <NA>         6         6         6         11
      pop_est pop_rank pop_year  gdp_md gdp_year                  economy
81  108116615       17     2019  376795     2019  5. Emerging region: G20
84    8776109       13     2019   24829     2019     6. Developing region
144 270625568       17     2019 1119190     2019 4. Emerging region: MIKT
                income_grp fips_10 iso_a2 iso_a2_eh iso_a3 iso_a3_eh iso_n3
81  4. Lower middle income      RP     PH        PH    PHL       PHL    608
84  4. Lower middle income      PP     PG        PG    PNG       PNG    598
144 4. Lower middle income      ID     ID        ID    IDN       IDN    360
    iso_n3_eh un_a3 wb_a2 wb_a3   woe_id woe_id_eh                   woe_note
81        608   608    PH   PHL 23424934  23424934 Exact WOE match as country
84        598   598    PG   PNG 23424926  23424926 Exact WOE match as country
144       360   360    ID   IDN 23424846  23424846 Exact WOE match as country
    adm0_iso adm0_diff adm0_tlc adm0_a3_us adm0_a3_fr adm0_a3_ru adm0_a3_es
81       PHL      <NA>      PHL        PHL        PHL        PHL        PHL
84       PN1      <NA>      PN1        PNG        PNG        PNG        PNG
144      IDN      <NA>      IDN        IDN        IDN        IDN        IDN
    adm0_a3_cn adm0_a3_tw adm0_a3_in adm0_a3_np adm0_a3_pk adm0_a3_de
81         PHL        PHL        PHL        PHL        PHL        PHL
84         PNG        PNG        PNG        PNG        PNG        PNG
144        IDN        IDN        IDN        IDN        IDN        IDN
    adm0_a3_gb adm0_a3_br adm0_a3_il adm0_a3_ps adm0_a3_sa adm0_a3_eg
81         PHL        PHL        PHL        PHL        PHL        PHL
84         PNG        PNG        PNG        PNG        PNG        PNG
144        IDN        IDN        IDN        IDN        IDN        IDN
    adm0_a3_ma adm0_a3_pt adm0_a3_ar adm0_a3_jp adm0_a3_ko adm0_a3_vn
81         PHL        PHL        PHL        PHL        PHL        PHL
84         PNG        PNG        PNG        PNG        PNG        PNG
144        IDN        IDN        IDN        IDN        IDN        IDN
    adm0_a3_tr adm0_a3_id adm0_a3_pl adm0_a3_gr adm0_a3_it adm0_a3_nl
81         PHL        PHL        PHL        PHL        PHL        PHL
84         PNG        PNG        PNG        PNG        PNG        PNG
144        IDN        IDN        IDN        IDN        IDN        IDN
    adm0_a3_se adm0_a3_bd adm0_a3_ua adm0_a3_un adm0_a3_wb continent region_un
81         PHL        PHL        PHL        -99        -99      Asia      Asia
84         PNG        PNG        PNG        -99        -99   Oceania   Oceania
144        IDN        IDN        IDN        -99        -99      Asia      Asia
             subregion           region_wb name_len long_len abbrev_len tiny
81  South-Eastern Asia East Asia & Pacific       11       11          5  -99
84           Melanesia East Asia & Pacific       16       16          6  -99
144 South-Eastern Asia East Asia & Pacific        9        9          5  -99
    homepart min_zoom min_label max_label  label_x   label_y      ne_id
81         1        0       2.5       7.0 122.4650 11.198000 1159321169
84         1        0       2.5       7.5 143.9102 -5.695285 1159321173
144        1        0       1.7       6.7 101.8929 -0.954404 1159320845
    wikidataid             name_ar       name_bn         name_de
81        Q928             الفلبين      ফিলিপাইন     Philippinen
84        Q691 بابوا غينيا الجديدة পাপুয়া নিউগিনি Papua-Neuguinea
144       Q252           إندونيسيا    ইন্দোনেশিয়া      Indonesien
             name_en            name_es       name_fa                   name_fr
81       Philippines          Filipinas       فیلیپین               Philippines
84  Papua New Guinea Papúa Nueva Guinea پاپوآ گینه نو Papouasie-Nouvelle-Guinée
144        Indonesia          Indonesia       اندونزی                 Indonésie
               name_el           name_he      name_hi         name_hu
81          Φιλιππίνες        הפיליפינים     फ़िलीपीन्स  Fülöp-szigetek
84  Παπούα Νέα Γουινέα פפואה גינאה החדשה पापुआ न्यू गिनी Pápua Új-Guinea
144          Ινδονησία         אינדונזיה     इंडोनेशिया       Indonézia
         name_id            name_it            name_ja      name_ko
81      Filipina          Filippine         フィリピン       필리핀
84  Papua Nugini Papua Nuova Guinea パプアニューギニア 파푸아뉴기니
144    Indonesia          Indonesia       インドネシア   인도네시아
                name_nl           name_pl          name_pt              name_ru
81           Filipijnen          Filipiny        Filipinas            Филиппины
84  Papoea-Nieuw-Guinea Papua-Nowa Gwinea Papua-Nova Guiné Папуа — Новая Гвинея
144           Indonesië         Indonezja        Indonésia            Индонезия
             name_sv         name_tr           name_uk       name_ur
81      Filippinerna      Filipinler         Філіппіни        فلپائن
84  Papua Nya Guinea Papua Yeni Gine Папуа Нова Гвінея پاپوا نیو گنی
144       Indonesien       Endonezya         Індонезія     انڈونیشیا
             name_vi        name_zh       name_zht      fclass_iso tlc_diff
81       Philippines         菲律宾         菲律賓 Admin-0 country     <NA>
84  Papua New Guinea 巴布亚新几内亚 巴布亞紐幾內亞 Admin-0 country     <NA>
144        Indonesia     印度尼西亚     印度尼西亞 Admin-0 country     <NA>
         fclass_tlc fclass_us fclass_fr fclass_ru fclass_es fclass_cn fclass_tw
81  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
84  Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
144 Admin-0 country      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
    fclass_in fclass_np fclass_pk fclass_de fclass_gb fclass_br fclass_il
81       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
84       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
144      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
    fclass_ps fclass_sa fclass_eg fclass_ma fclass_pt fclass_ar fclass_jp
81       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
84       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
144      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
    fclass_ko fclass_vn fclass_tr fclass_id fclass_pl fclass_gr fclass_it
81       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
84       <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
144      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>      <NA>
    fclass_nl fclass_se fclass_bd fclass_ua                       geometry
81       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((121.1016 18...
84       <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((152.9658 -4...
144      <NA>      <NA>      <NA>      <NA> MULTIPOLYGON (((97.48154 1....
nrow(ng_sf)
[1] 3
plot(ng_sf$geometry)

ngp <- ggplot(ng_sf) +
  geom_sf(color = "grey80", fill = "#f2f2f2", linewidth = 0.25) +
  coord_sf(xlim = c(120, 154), ylim = c(-12, 10)) +
  theme( axis.title = element_blank(),
         panel.background = element_rect(fill = "lightblue", colour = NA)
       )

Add points to map - first get data

My metadata file contains many genera that I am not interested in so first I filter by genus of interest.

d <- read.csv("https://raw.githubusercontent.com/mbutler808/rclass-2025/refs/heads/main/posts/2025-05-01-spatial/metadata.csv")

odat <- d %>% filter(genus=="Oreophryne")
adat <- d %>% filter(genus=="Auparoparo"|genus=="Aphantophryne")

I make two separate datasets just by choice and for convenience to plot them in different colors later. I could also do it by keeping it together and filtering, personal choice.

# convert to sf
osf <- st_as_sf(odat, coords=c("longitude", "latitude"))
asf <- st_as_sf(adat, coords=c("longitude", "latitude"))

Map with points in different shapes and colors.

ngpdat <- ngp +
  geom_point(data=adat, 
    aes(x=longitude, y=latitude), 
    colour="red", 
    pch="+", 
    size=8, 
    alpha=.75) +
  geom_point(data=odat, 
    aes(x=longitude, y=latitude), 
    colour="grey50", 
    fill="blue", 
    pch=24, 
    size=2, 
    alpha=.75) 

Want to make a zoomed-in inset - identify coordinates

ngpdat +
  geom_hline(yintercept = -8.75, lty = 2, colour = "red") +
  geom_hline(yintercept = -12, lty = 2, colour = "red") +
  geom_vline(xintercept = 147, lty = 2, colour = "red") +
  geom_vline(xintercept = 155, lty = 2, colour = "red") 

Annotate plot with a rectangle using the coordinates

ngpdat <- ngpdat +
  annotate("rect", xmin = 149, xmax = 154.5, ymin = -12, ymax = -8.75, color = "grey50", fill = NA) +
  theme_void() +
  theme(axis.title = element_blank(),
        panel.background = element_rect(fill = "lightblue", colour = NA)
        )

Make inset - basically another plot zoomed in

inset <-  
  ggplot(ng_sf) + 
  geom_sf(color = "grey70", fill = "#f2f2f2", linewidth = 0.25) +
  coord_sf(xlim = c(149, 154.5), ylim = c(-12, -8.75), expand=FALSE) + #expand=F prevents ggplot from expanding slightly beyond the limits
  filter(d, genus=="Auparoparo" & (latitude > -12 &
    latitude < -8.75 &
    longitude > 149 & 
    longitude < 154.5) 
    ) %>% geom_point( 
      mapping = aes(x=longitude, y=latitude), 
      color="red", 
      pch="+", 
      size=8, 
      alpha=.75) +
  filter(d, genus=="Oreophryne" & (latitude > -12 &
    latitude < -8.75 &
    longitude > 149 & 
    longitude < 154.5) 
    ) %>%geom_point(
        mapping = aes(x=longitude, y=latitude), 
        colour="grey50", 
      fill="blue", 
      pch=24, 
      size=3, 
      alpha=.75) +
  annotate("rect", xmin = 149, xmax = 154.5, ymin = -12, ymax = -8.75, color = "grey50", fill=NA, linewidth=1.5) +
  labs(x = NULL, y = NULL) + 
  xlim(149, 154.5) +
  ylim(-12, -8.75) +
  theme_void() +
  theme(axis.title = element_blank(),
        panel.background = element_rect(fill = "lightblue", colour = NA)) +
  guides(size = "none") 

Plot the baseplot

baseplot <- ngpdat + 
  geom_sf_text(aes(label=name_en),
    size = 3,
    fontface = "bold",
    family = "serif",
    color = "grey30",
    nudge_x = c(0,0,25),
    nudge_y = c(0,0,-4)) +
  #Add scale bar to bottom left from ggspatial
    annotation_scale(location = "bl", 
      height = unit(.25, "cm"), 
      pad_x = unit(0.3, "in"), 
      pad_y = unit(0.3, "in")) +
    #Add north arrow to bottom left from ggspatial
    annotation_north_arrow(height = unit(1, "cm"), 
      width = unit(1, "cm"),
      which_north = "true", 
      location = "bl", 
      pad_x = unit(2, "in"), 
      pad_y = unit(0.5, "in")) 

I will use the grid package to make an inset with the viewport function.

vp <- viewport( x=0.75,   # from grid package
        y=0.75,
                width=unit(3, "inches"),
                height=unit(2,"inches"))
grid.show.viewport(vp)

print(baseplot)
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data
print(inset, vp = viewport(0.75, 0.78, width = 0.4, height = 0.4))  

Write to an external file

There are two print statements here after starting the png device (you can also turn on a pdf if you wish)

height=6
width=9

png(file="map.png", # for png format you have to specify 
                    # size and resolution
    height=height, 
    width=width, 
    units="in", 
    res=300)
      print(baseplot) 
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data
      print(inset, vp = viewport(0.75, 0.78, width = 0.4, height = 0.4))  
dev.off()
quartz_off_screen 
                2 

Cool tip!

scales::show_col(c("#9DBF9E", "#A84268", "#FCB97D", "#C0BCB5", "#4A6C6F", "#FF5E5B"))

Great Mapping Blogs