library(readr)
species_data <- read_csv("Mesoamerica.csv")
## Rows: 8 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Name
## dbl (9): Mammals, Birds, Reptiles*, Amphibians, Fishes*, Molluscs*, Other In...
## num (2): Plants*, Total
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(species_data)
## # A tibble: 6 × 12
##   Name        Mammals Birds `Reptiles*` Amphibians `Fishes*` `Molluscs*`
##   <chr>         <dbl> <dbl>       <dbl>      <dbl>     <dbl>       <dbl>
## 1 Belize           10     7           8          2        67           0
## 2 Costa Rica       12    24          16         56       138           2
## 3 El Salvador       6     9          10          8        52           0
## 4 Guatemala        16    18          39         96        98           2
## 5 Honduras         10    17          60         70        89           0
## 6 Mexico           97    67         106        232       305          14
## # ℹ 5 more variables: `Other Inverts*` <dbl>, `Plants*` <dbl>, `Fungi*` <dbl>,
## #   `Chromists*` <dbl>, Total <dbl>
# Clean all of the column names
colnames(species_data)
##  [1] "Name"           "Mammals"        "Birds"          "Reptiles*"     
##  [5] "Amphibians"     "Fishes*"        "Molluscs*"      "Other Inverts*"
##  [9] "Plants*"        "Fungi*"         "Chromists*"     "Total"
colnames(species_data) <- gsub("\\.", "_", colnames(species_data))
colnames(species_data)
##  [1] "Name"           "Mammals"        "Birds"          "Reptiles*"     
##  [5] "Amphibians"     "Fishes*"        "Molluscs*"      "Other Inverts*"
##  [9] "Plants*"        "Fungi*"         "Chromists*"     "Total"
# Remove asterisks from column names
colnames(species_data) <- gsub("\\*", "", colnames(species_data))
# View cleaned column names
colnames(species_data)
##  [1] "Name"          "Mammals"       "Birds"         "Reptiles"     
##  [5] "Amphibians"    "Fishes"        "Molluscs"      "Other Inverts"
##  [9] "Plants"        "Fungi"         "Chromists"     "Total"
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
# Read and view shapefile
map_data <- st_read("/Users/rokayaaqrabawi/Documents/EMES_203_Homework/ne_110m_admin_0_countries.shp")
## Reading layer `ne_110m_admin_0_countries' from data source 
##   `/Users/rokayaaqrabawi/Documents/EMES_203_Homework/ne_110m_admin_0_countries.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 177 features and 168 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -90 xmax: 180 ymax: 83.64513
## Geodetic CRS:  WGS 84
unique(map_data$ADMIN)
##   [1] "Fiji"                               
##   [2] "United Republic of Tanzania"        
##   [3] "Western Sahara"                     
##   [4] "Canada"                             
##   [5] "United States of America"           
##   [6] "Kazakhstan"                         
##   [7] "Uzbekistan"                         
##   [8] "Papua New Guinea"                   
##   [9] "Indonesia"                          
##  [10] "Argentina"                          
##  [11] "Chile"                              
##  [12] "Democratic Republic of the Congo"   
##  [13] "Somalia"                            
##  [14] "Kenya"                              
##  [15] "Sudan"                              
##  [16] "Chad"                               
##  [17] "Haiti"                              
##  [18] "Dominican Republic"                 
##  [19] "Russia"                             
##  [20] "The Bahamas"                        
##  [21] "Falkland Islands"                   
##  [22] "Norway"                             
##  [23] "Greenland"                          
##  [24] "French Southern and Antarctic Lands"
##  [25] "East Timor"                         
##  [26] "South Africa"                       
##  [27] "Lesotho"                            
##  [28] "Mexico"                             
##  [29] "Uruguay"                            
##  [30] "Brazil"                             
##  [31] "Bolivia"                            
##  [32] "Peru"                               
##  [33] "Colombia"                           
##  [34] "Panama"                             
##  [35] "Costa Rica"                         
##  [36] "Nicaragua"                          
##  [37] "Honduras"                           
##  [38] "El Salvador"                        
##  [39] "Guatemala"                          
##  [40] "Belize"                             
##  [41] "Venezuela"                          
##  [42] "Guyana"                             
##  [43] "Suriname"                           
##  [44] "France"                             
##  [45] "Ecuador"                            
##  [46] "Puerto Rico"                        
##  [47] "Jamaica"                            
##  [48] "Cuba"                               
##  [49] "Zimbabwe"                           
##  [50] "Botswana"                           
##  [51] "Namibia"                            
##  [52] "Senegal"                            
##  [53] "Mali"                               
##  [54] "Mauritania"                         
##  [55] "Benin"                              
##  [56] "Niger"                              
##  [57] "Nigeria"                            
##  [58] "Cameroon"                           
##  [59] "Togo"                               
##  [60] "Ghana"                              
##  [61] "Ivory Coast"                        
##  [62] "Guinea"                             
##  [63] "Guinea-Bissau"                      
##  [64] "Liberia"                            
##  [65] "Sierra Leone"                       
##  [66] "Burkina Faso"                       
##  [67] "Central African Republic"           
##  [68] "Republic of the Congo"              
##  [69] "Gabon"                              
##  [70] "Equatorial Guinea"                  
##  [71] "Zambia"                             
##  [72] "Malawi"                             
##  [73] "Mozambique"                         
##  [74] "eSwatini"                           
##  [75] "Angola"                             
##  [76] "Burundi"                            
##  [77] "Israel"                             
##  [78] "Lebanon"                            
##  [79] "Madagascar"                         
##  [80] "Palestine"                          
##  [81] "Gambia"                             
##  [82] "Tunisia"                            
##  [83] "Algeria"                            
##  [84] "Jordan"                             
##  [85] "United Arab Emirates"               
##  [86] "Qatar"                              
##  [87] "Kuwait"                             
##  [88] "Iraq"                               
##  [89] "Oman"                               
##  [90] "Vanuatu"                            
##  [91] "Cambodia"                           
##  [92] "Thailand"                           
##  [93] "Laos"                               
##  [94] "Myanmar"                            
##  [95] "Vietnam"                            
##  [96] "North Korea"                        
##  [97] "South Korea"                        
##  [98] "Mongolia"                           
##  [99] "India"                              
## [100] "Bangladesh"                         
## [101] "Bhutan"                             
## [102] "Nepal"                              
## [103] "Pakistan"                           
## [104] "Afghanistan"                        
## [105] "Tajikistan"                         
## [106] "Kyrgyzstan"                         
## [107] "Turkmenistan"                       
## [108] "Iran"                               
## [109] "Syria"                              
## [110] "Armenia"                            
## [111] "Sweden"                             
## [112] "Belarus"                            
## [113] "Ukraine"                            
## [114] "Poland"                             
## [115] "Austria"                            
## [116] "Hungary"                            
## [117] "Moldova"                            
## [118] "Romania"                            
## [119] "Lithuania"                          
## [120] "Latvia"                             
## [121] "Estonia"                            
## [122] "Germany"                            
## [123] "Bulgaria"                           
## [124] "Greece"                             
## [125] "Turkey"                             
## [126] "Albania"                            
## [127] "Croatia"                            
## [128] "Switzerland"                        
## [129] "Luxembourg"                         
## [130] "Belgium"                            
## [131] "Netherlands"                        
## [132] "Portugal"                           
## [133] "Spain"                              
## [134] "Ireland"                            
## [135] "New Caledonia"                      
## [136] "Solomon Islands"                    
## [137] "New Zealand"                        
## [138] "Australia"                          
## [139] "Sri Lanka"                          
## [140] "China"                              
## [141] "Taiwan"                             
## [142] "Italy"                              
## [143] "Denmark"                            
## [144] "United Kingdom"                     
## [145] "Iceland"                            
## [146] "Azerbaijan"                         
## [147] "Georgia"                            
## [148] "Philippines"                        
## [149] "Malaysia"                           
## [150] "Brunei"                             
## [151] "Slovenia"                           
## [152] "Finland"                            
## [153] "Slovakia"                           
## [154] "Czechia"                            
## [155] "Eritrea"                            
## [156] "Japan"                              
## [157] "Paraguay"                           
## [158] "Yemen"                              
## [159] "Saudi Arabia"                       
## [160] "Antarctica"                         
## [161] "Northern Cyprus"                    
## [162] "Cyprus"                             
## [163] "Morocco"                            
## [164] "Egypt"                              
## [165] "Libya"                              
## [166] "Ethiopia"                           
## [167] "Djibouti"                           
## [168] "Somaliland"                         
## [169] "Uganda"                             
## [170] "Rwanda"                             
## [171] "Bosnia and Herzegovina"             
## [172] "North Macedonia"                    
## [173] "Republic of Serbia"                 
## [174] "Montenegro"                         
## [175] "Kosovo"                             
## [176] "Trinidad and Tobago"                
## [177] "South Sudan"
# View unique country names in species dataset
unique(species_data$Name)
## [1] "Belize"      "Costa Rica"  "El Salvador" "Guatemala"   "Honduras"   
## [6] "Mexico"      "Nicaragua"   "Panama"
# Filter map data
filtered_map <- map_data[map_data$ADMIN %in% species_data$Name, ]
# View filtered map data
unique(filtered_map$ADMIN)
## [1] "Mexico"      "Panama"      "Costa Rica"  "Nicaragua"   "Honduras"   
## [6] "El Salvador" "Guatemala"   "Belize"
# Merge species data and map data
merged_data <- merge(filtered_map, species_data, by.x = "ADMIN", by.y = "Name")
# View merged data 
head(merged_data)
## Simple feature collection with 6 features and 179 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -117.1278 ymin: 8.225028 xmax: -82.5462 ymax: 32.72083
## Geodetic CRS:  WGS 84
##         ADMIN      featurecla scalerank LABELRANK  SOVEREIGNT SOV_A3 ADM0_DIF
## 1      Belize Admin-0 country         1         6      Belize    BLZ        0
## 2  Costa Rica Admin-0 country         1         5  Costa Rica    CRI        0
## 3 El Salvador Admin-0 country         1         6 El Salvador    SLV        0
## 4   Guatemala Admin-0 country         1         3   Guatemala    GTM        0
## 5    Honduras Admin-0 country         1         5    Honduras    HND        0
## 6      Mexico Admin-0 country         1         2      Mexico    MEX        0
##   LEVEL              TYPE TLC ADM0_A3 GEOU_DIF     GEOUNIT GU_A3 SU_DIF
## 1     2 Sovereign country   1     BLZ        0      Belize   BLZ      0
## 2     2 Sovereign country   1     CRI        0  Costa Rica   CRI      0
## 3     2 Sovereign country   1     SLV        0 El Salvador   SLV      0
## 4     2 Sovereign country   1     GTM        0   Guatemala   GTM      0
## 5     2 Sovereign country   1     HND        0    Honduras   HND      0
## 6     2 Sovereign country   1     MEX        0      Mexico   MEX      0
##       SUBUNIT SU_A3 BRK_DIFF        NAME   NAME_LONG BRK_A3    BRK_NAME
## 1      Belize   BLZ        0      Belize      Belize    BLZ      Belize
## 2  Costa Rica   CRI        0  Costa Rica  Costa Rica    CRI  Costa Rica
## 3 El Salvador   SLV        0 El Salvador El Salvador    SLV El Salvador
## 4   Guatemala   GTM        0   Guatemala   Guatemala    GTM   Guatemala
## 5    Honduras   HND        0    Honduras    Honduras    HND    Honduras
## 6      Mexico   MEX        0      Mexico      Mexico    MEX      Mexico
##   BRK_GROUP ABBREV POSTAL               FORMAL_EN FORMAL_FR  NAME_CIAWF
## 1      <NA> Belize     BZ                  Belize      <NA>      Belize
## 2      <NA>   C.R.     CR  Republic of Costa Rica      <NA>  Costa Rica
## 3      <NA> El. S.     SV Republic of El Salvador      <NA> El Salvador
## 4      <NA>  Guat.     GT   Republic of Guatemala      <NA>   Guatemala
## 5      <NA>  Hond.     HN    Republic of Honduras      <NA>    Honduras
## 6      <NA>   Mex.     MX   United Mexican States      <NA>      Mexico
##   NOTE_ADM0 NOTE_BRK   NAME_SORT NAME_ALT MAPCOLOR7 MAPCOLOR8 MAPCOLOR9
## 1      <NA>     <NA>      Belize     <NA>         1         4         5
## 2      <NA>     <NA>  Costa Rica     <NA>         3         2         4
## 3      <NA>     <NA> El Salvador     <NA>         1         4         6
## 4      <NA>     <NA>   Guatemala     <NA>         3         3         3
## 5      <NA>     <NA>    Honduras     <NA>         2         5         2
## 6      <NA>     <NA>      Mexico     <NA>         6         1         7
##   MAPCOLOR13   POP_EST POP_RANK POP_YEAR  GDP_MD GDP_YEAR
## 1          7    390353       10     2019    1879     2019
## 2          2   5047561       13     2019   61801     2019
## 3          8   6453553       13     2019   27022     2019
## 4          6  16604026       14     2019   76710     2019
## 5          5   9746117       13     2019   25095     2019
## 6          3 127575529       17     2019 1268870     2019
##                    ECONOMY             INCOME_GRP FIPS_10 ISO_A2 ISO_A2_EH
## 1     6. Developing region 4. Lower middle income      BH     BZ        BZ
## 2  5. Emerging region: G20 3. Upper middle income      CS     CR        CR
## 3     6. Developing region 4. Lower middle income      ES     SV        SV
## 4     6. Developing region 4. Lower middle income      GT     GT        GT
## 5     6. Developing region 4. Lower middle income      HO     HN        HN
## 6 4. Emerging region: MIKT 3. Upper middle income      MX     MX        MX
##   ISO_A3 ISO_A3_EH ISO_N3 ISO_N3_EH UN_A3 WB_A2 WB_A3   WOE_ID WOE_ID_EH
## 1    BLZ       BLZ    084       084   084    BZ   BLZ 23424760  23424760
## 2    CRI       CRI    188       188   188    CR   CRI 23424791  23424791
## 3    SLV       SLV    222       222   222    SV   SLV 23424807  23424807
## 4    GTM       GTM    320       320   320    GT   GTM 23424834  23424834
## 5    HND       HND    340       340   340    HN   HND 23424841  23424841
## 6    MEX       MEX    484       484   484    MX   MEX 23424900  23424900
##                     WOE_NOTE ADM0_ISO ADM0_DIFF ADM0_TLC ADM0_A3_US ADM0_A3_FR
## 1 Exact WOE match as country      BLZ      <NA>      BLZ        BLZ        BLZ
## 2 Exact WOE match as country      CRI      <NA>      CRI        CRI        CRI
## 3 Exact WOE match as country      SLV      <NA>      SLV        SLV        SLV
## 4 Exact WOE match as country      GTM      <NA>      GTM        GTM        GTM
## 5 Exact WOE match as country      HND      <NA>      HND        HND        HND
## 6 Exact WOE match as country      MEX      <NA>      MEX        MEX        MEX
##   ADM0_A3_RU ADM0_A3_ES ADM0_A3_CN ADM0_A3_TW ADM0_A3_IN ADM0_A3_NP ADM0_A3_PK
## 1        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ
## 2        CRI        CRI        CRI        CRI        CRI        CRI        CRI
## 3        SLV        SLV        SLV        SLV        SLV        SLV        SLV
## 4        GTM        GTM        GTM        GTM        GTM        GTM        GTM
## 5        HND        HND        HND        HND        HND        HND        HND
## 6        MEX        MEX        MEX        MEX        MEX        MEX        MEX
##   ADM0_A3_DE ADM0_A3_GB ADM0_A3_BR ADM0_A3_IL ADM0_A3_PS ADM0_A3_SA ADM0_A3_EG
## 1        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ
## 2        CRI        CRI        CRI        CRI        CRI        CRI        CRI
## 3        SLV        SLV        SLV        SLV        SLV        SLV        SLV
## 4        GTM        GTM        GTM        GTM        GTM        GTM        GTM
## 5        HND        HND        HND        HND        HND        HND        HND
## 6        MEX        MEX        MEX        MEX        MEX        MEX        MEX
##   ADM0_A3_MA ADM0_A3_PT ADM0_A3_AR ADM0_A3_JP ADM0_A3_KO ADM0_A3_VN ADM0_A3_TR
## 1        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ
## 2        CRI        CRI        CRI        CRI        CRI        CRI        CRI
## 3        SLV        SLV        SLV        SLV        SLV        SLV        SLV
## 4        GTM        GTM        GTM        GTM        GTM        GTM        GTM
## 5        HND        HND        HND        HND        HND        HND        HND
## 6        MEX        MEX        MEX        MEX        MEX        MEX        MEX
##   ADM0_A3_ID ADM0_A3_PL ADM0_A3_GR ADM0_A3_IT ADM0_A3_NL ADM0_A3_SE ADM0_A3_BD
## 1        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ        BLZ
## 2        CRI        CRI        CRI        CRI        CRI        CRI        CRI
## 3        SLV        SLV        SLV        SLV        SLV        SLV        SLV
## 4        GTM        GTM        GTM        GTM        GTM        GTM        GTM
## 5        HND        HND        HND        HND        HND        HND        HND
## 6        MEX        MEX        MEX        MEX        MEX        MEX        MEX
##   ADM0_A3_UA ADM0_A3_UN ADM0_A3_WB     CONTINENT REGION_UN       SUBREGION
## 1        BLZ        -99        -99 North America  Americas Central America
## 2        CRI        -99        -99 North America  Americas Central America
## 3        SLV        -99        -99 North America  Americas Central America
## 4        GTM        -99        -99 North America  Americas Central America
## 5        HND        -99        -99 North America  Americas Central America
## 6        MEX        -99        -99 North America  Americas Central America
##                   REGION_WB NAME_LEN LONG_LEN ABBREV_LEN TINY HOMEPART MIN_ZOOM
## 1 Latin America & Caribbean        6        6          6  -99        1        0
## 2 Latin America & Caribbean       10       10          4  -99        1        0
## 3 Latin America & Caribbean       11       11          6  -99        1        0
## 4 Latin America & Caribbean        9        9          5    4        1        0
## 5 Latin America & Caribbean        8        8          5  -99        1        0
## 6 Latin America & Caribbean        6        6          4  -99        1        0
##   MIN_LABEL MAX_LABEL    LABEL_X  LABEL_Y      NE_ID WIKIDATAID   NAME_AR
## 1       5.0      10.0  -88.71296 17.20207 1159320431       Q242      بليز
## 2       2.5       8.0  -84.07792 10.06510 1159320525       Q800 كوستاريكا
## 3       5.0      10.0  -88.89012 13.68537 1159321253       Q792 السلفادور
## 4       3.0       8.0  -90.49713 14.98213 1159320815       Q774 غواتيمالا
## 5       4.5       9.5  -86.88760 14.79480 1159320827       Q783   هندوراس
## 6       2.0       6.7 -102.28945 23.91999 1159321055        Q96   المكسيك
##       NAME_BN     NAME_DE     NAME_EN     NAME_ES    NAME_FA    NAME_FR
## 1       বেলিজ      Belize      Belize      Belice       بلیز     Belize
## 2  কোস্টা রিকা  Costa Rica  Costa Rica  Costa Rica  کاستاریکا Costa Rica
## 3 এল সালভাদোর El Salvador El Salvador El Salvador السالوادور   Salvador
## 4   গুয়াতেমালা   Guatemala   Guatemala   Guatemala   گواتمالا  Guatemala
## 5      হন্ডুরাস    Honduras    Honduras    Honduras    هندوراس   Honduras
## 6     মেক্সিকো      Mexiko      Mexico      México      مکزیک    Mexique
##       NAME_EL    NAME_HE     NAME_HI    NAME_HU     NAME_ID     NAME_IT
## 1      Μπελίζ       בליז        बेलीज़     Belize      Belize      Belize
## 2  Κόστα Ρίκα קוסטה ריקה  कोस्टा रीका Costa Rica  Kosta Rika  Costa Rica
## 3 Ελ Σαλβαδόρ אל סלוודור अल साल्वाडोर   Salvador El Salvador El Salvador
## 4  Γουατεμάλα    גואטמלה    ग्वाटेमाला  Guatemala   Guatemala   Guatemala
## 5     Ονδούρα    הונדורס      हौण्डुरस   Honduras    Honduras    Honduras
## 6      Μεξικό     מקסיקו      मेक्सिको     Mexikó     Meksiko     Messico
##          NAME_JA    NAME_KO     NAME_NL   NAME_PL     NAME_PT    NAME_RU
## 1       ベリーズ     벨리즈      Belize    Belize      Belize      Белиз
## 2     コスタリカ 코스타리카  Costa Rica Kostaryka  Costa Rica Коста-Рика
## 3 エルサルバドル 엘살바도르 El Salvador  Salwador El Salvador  Сальвадор
## 4     グアテマラ   과테말라   Guatemala Gwatemala   Guatemala  Гватемала
## 5   ホンジュラス   온두라스    Honduras  Honduras    Honduras   Гондурас
## 6       メキシコ     멕시코      Mexico    Meksyk      México    Мексика
##       NAME_SV     NAME_TR    NAME_UK      NAME_UR     NAME_VI    NAME_ZH
## 1      Belize      Belize      Беліз        بیلیز      Belize     伯利兹
## 2  Costa Rica  Kosta Rika Коста-Рика    کوسٹاریکا  Costa Rica 哥斯达黎加
## 3 El Salvador El Salvador  Сальвадор ایل سیلواڈور El Salvador   萨尔瓦多
## 4   Guatemala   Guatemala  Гватемала    گواتیمالا   Guatemala   危地马拉
## 5    Honduras    Honduras   Гондурас     ہونڈوراس    Honduras   洪都拉斯
## 6      Mexiko     Meksika    Мексика      میکسیکو      México     墨西哥
##     NAME_ZHT      FCLASS_ISO TLC_DIFF      FCLASS_TLC FCLASS_US FCLASS_FR
## 1     貝里斯 Admin-0 country     <NA> Admin-0 country      <NA>      <NA>
## 2 哥斯大黎加 Admin-0 country     <NA> Admin-0 country      <NA>      <NA>
## 3   薩爾瓦多 Admin-0 country     <NA> Admin-0 country      <NA>      <NA>
## 4   瓜地馬拉 Admin-0 country     <NA> Admin-0 country      <NA>      <NA>
## 5   宏都拉斯 Admin-0 country     <NA> Admin-0 country      <NA>      <NA>
## 6     墨西哥 Admin-0 country     <NA> Admin-0 country      <NA>      <NA>
##   FCLASS_RU FCLASS_ES FCLASS_CN FCLASS_TW FCLASS_IN FCLASS_NP FCLASS_PK
## 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>
##   FCLASS_DE FCLASS_GB FCLASS_BR FCLASS_IL FCLASS_PS FCLASS_SA FCLASS_EG
## 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>
##   FCLASS_MA FCLASS_PT FCLASS_AR FCLASS_JP FCLASS_KO FCLASS_VN FCLASS_TR
## 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>
##   FCLASS_ID FCLASS_PL FCLASS_GR FCLASS_IT FCLASS_NL FCLASS_SE FCLASS_BD
## 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>
##   FCLASS_UA Mammals Birds Reptiles Amphibians Fishes Molluscs Other Inverts
## 1      <NA>      10     7        8          2     67        0            27
## 2      <NA>      12    24       16         56    138        2            57
## 3      <NA>       6     9       10          8     52        0            11
## 4      <NA>      16    18       39         96     98        2            36
## 5      <NA>      10    17       60         70     89        0            36
## 6      <NA>      97    67      106        232    305       14           152
##   Plants Fungi Chromists Total                       geometry
## 1    114     0         0   235 MULTIPOLYGON (((-89.14308 1...
## 2    463     6         0   774 MULTIPOLYGON (((-82.5462 9....
## 3    106     0         0   202 MULTIPOLYGON (((-89.35333 1...
## 4    470     1         0   776 MULTIPOLYGON (((-92.22775 1...
## 5    214     2         0   498 MULTIPOLYGON (((-83.14722 1...
## 6   1632     7         0  2614 MULTIPOLYGON (((-117.1278 3...
library(ggplot2)

# Create map
ggplot(data = merged_data) +
  geom_sf(aes(fill = Total)) + 
  scale_fill_gradient(low = "pink", high = "darkblue", name = "Threatened Species") +
  labs(title = "Total Threatened Species in Mesoamerica", 
       caption = "Source: IUCN Red List") +
  theme_minimal()

# Define the function
plotspecies <- function(species_data, map_data, column_name) {
  # Merge datasets
  merged_data <- merge(map_data, species_data, by.x = "ADMIN", by.y = "Name")
  
  # Create map
  ggplot(data = merged_data) +
    geom_sf(aes_string(fill = column_name)) +
    scale_fill_gradient(low = "pink", high = "darkblue", name = paste(column_name, "Species")) +
    labs(title = paste("Threatened", column_name, "in Mesoamerica"),
         caption = "Source: IUCN Red List") +
    theme_minimal()
}

# Test "Mammals"
plotspecies(species_data, filtered_map, "Mammals")
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Bird Plot
plotspecies(species_data, filtered_map, "Birds")

# Fish Plot
plotspecies(species_data, filtered_map, "Fishes")

# Plant Plot
plotspecies(species_data, filtered_map, "Plants")

# Find the country with most threatened species per category
most_total <- species_data[which.max(species_data$Total), "Name"]
most_mammals <- species_data[which.max(species_data$Mammals), "Name"]
most_birds <- species_data[which.max(species_data$Birds), "Name"]
most_fishes <- species_data[which.max(species_data$Fishes), "Name"]
most_plants <- species_data[which.max(species_data$Plants), "Name"]

# Print results
list(
  Most_Threatened_Total = most_total,
  Most_Threatened_Mammals = most_mammals,
  Most_Threatened_Birds = most_birds,
  Most_Threatened_Fishes = most_fishes,
  Most_Threatened_Plants = most_plants
)
## $Most_Threatened_Total
## # A tibble: 1 × 1
##   Name  
##   <chr> 
## 1 Mexico
## 
## $Most_Threatened_Mammals
## # A tibble: 1 × 1
##   Name  
##   <chr> 
## 1 Mexico
## 
## $Most_Threatened_Birds
## # A tibble: 1 × 1
##   Name  
##   <chr> 
## 1 Mexico
## 
## $Most_Threatened_Fishes
## # A tibble: 1 × 1
##   Name  
##   <chr> 
## 1 Mexico
## 
## $Most_Threatened_Plants
## # A tibble: 1 × 1
##   Name  
##   <chr> 
## 1 Mexico

Observations

Conclusion

Mexico is leading in the number of threatened species across all categories shows its role as a biodiversity hotspot, with diverse ecosystems ranging from deserts and forests to coastal regions and marine environments. Habitat loss, pollution, climate change, and urbanization continue to put pressure on its organisms. The findings demonstrate the need for conservation efforts to Mexico’s ecosystems and the other ecosystems in Mesoamerica. Protecting Mexico’s biodiversity is not only essential for preserving its natural heritage but also for maintaining the ecological balance critical to the region’s resilience in the face of global environmental change.

With this, your assignment is nearly complete! Let me know if you need help refining anything or if you’re ready to knit your R Markdown file into an HTML report.