#mutation piechart plots
library(tidyverse) # ggplot2, dplyr, tidyr, readr, purrr, tibble
## -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v ggplot2 3.3.6      v purrr   0.3.4 
## v tibble  3.1.8      v dplyr   1.0.10
## v tidyr   1.2.1      v stringr 1.4.1 
## v readr   2.1.3      v forcats 0.5.2 
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(rnaturalearth) 
library(rnaturalearthdata)
library(sf) #see ubuntu issues here: https://rtask.thinkr.fr/installation-of-r-4-0-on-ubuntu-20-04-lts-and-tips-for-spatial-packages/
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(ggspatial)
library(scatterpie)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
#load base map layers
admin10 <- ne_download(scale="large", type = "admin_1_states_provinces_lines",
                       category = "cultural", returnclass = "sf")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Samuel\AppData\Local\Temp\RtmpUXzRpF", layer: "ne_10m_admin_1_states_provinces_lines"
## with 10179 features
## It has 57 fields
## Integer64 fields read as strings:  ne_id
rivers10 <- ne_download(scale = 10, type = 'rivers_lake_centerlines', 
                        category = 'physical', returnclass = "sf")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Samuel\AppData\Local\Temp\RtmpUXzRpF", layer: "ne_10m_rivers_lake_centerlines"
## with 1473 features
## It has 38 fields
## Integer64 fields read as strings:  ne_id
lakes10 <- ne_download(scale = "large", type = 'lakes', 
                       category = 'physical', returnclass = "sf")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Samuel\AppData\Local\Temp\RtmpUXzRpF", layer: "ne_10m_lakes"
## with 1355 features
## It has 41 fields
## Integer64 fields read as strings:  scalerank ne_id
sov110 <- ne_download(scale="medium", type = "sovereignty",
                      category = "cultural", returnclass = "sf")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Samuel\AppData\Local\Temp\RtmpUXzRpF", layer: "ne_50m_admin_0_sovereignty"
## with 201 features
## It has 168 fields
## Integer64 fields read as strings:  NE_ID
admin110 <- ne_download(scale="large", type = "populated_places",
                        category = "cultural", returnclass = "sf")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Samuel\AppData\Local\Temp\RtmpUXzRpF", layer: "ne_10m_populated_places"
## with 7342 features
## It has 137 fields
## Integer64 fields read as strings:  POP_MAX POP_MIN POP_OTHER MAX_POP10 MAX_POP20 MAX_POP50 MAX_POP300 MAX_POP310 MAX_NATSCA MIN_AREAKM MAX_AREAKM MIN_AREAMI MAX_AREAMI MIN_PERKM MAX_PERKM MIN_PERMI MAX_PERMI POP1950 POP1955 POP1960 POP1965 POP1970 POP1975 POP1980 POP1985 POP1990 POP1995 POP2000 POP2005 POP2010 POP2015 POP2020 POP2025 POP2050 WOF_ID NE_ID GEONAMESID
#scale pies by sample size
mosq_abundance<- read.csv("D:/Jeremiah/template species abundance csv.csv")
mosq_abundance$radius <- rescale(mosq_abundance$total_mosq, to = c(2, 5)) 
#plot map
 Plot <- ggplot()+
  geom_sf(data=sov110, color='black', size=0.8, fill = ifelse(sov110$ADMIN == "Kenya", 'grey98', 'grey90')) +
  geom_sf(data=rivers10, color="cyan4", size=0.5, alpha=0.5) +
  geom_sf(data=lakes10, color="grey40", fill ="aliceblue", size= 0.8) +
  geom_sf(data=admin10, color="grey80", size= 0.4) +
  geom_sf(data=admin110, color="grey80", size= 0.4) +
geom_scatterpie(aes(x=longitude, y=latitude, r = 0.1), 
                  data = mosq_abundance, 
                  cols = c("funestus_prop", "gambiae_prop"), 
                  color = "grey20",
                  legend_name = "Mosq_Prop",
                  alpha=.8)+
  scale_fill_manual(values = c("darkgoldenrod1","firebrick")) +
  coord_sf(xlim = c(33,45 ), ylim = c(-4,5.5), expand = TRUE) +
  theme_void()+
  theme(panel.background = element_rect(fill = 'aliceblue', colour = 'grey97'))
 Plot

#save plot
ggsave("D:/Jeremiah/Ke_Mosq.png", dpi=600, width=7.5, height=7)

Session Info:

sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22000)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_Kenya.1252  LC_CTYPE=English_Kenya.1252   
## [3] LC_MONETARY=English_Kenya.1252 LC_NUMERIC=C                  
## [5] LC_TIME=English_Kenya.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] scales_1.2.1            scatterpie_0.1.8        ggspatial_1.1.6        
##  [4] sf_1.0-8                rnaturalearthdata_0.1.0 rnaturalearth_0.1.0    
##  [7] forcats_0.5.2           stringr_1.4.1           dplyr_1.0.10           
## [10] purrr_0.3.4             readr_2.1.3             tidyr_1.2.1            
## [13] tibble_3.1.8            ggplot2_3.3.6           tidyverse_1.3.2        
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.2            lubridate_1.8.0     httr_1.4.4         
##  [4] tools_4.1.2         backports_1.4.1     bslib_0.4.0        
##  [7] utf8_1.2.2          rgdal_1.5-32        R6_2.5.1           
## [10] KernSmooth_2.23-20  DBI_1.1.3           colorspace_2.0-3   
## [13] withr_2.5.0         sp_1.5-0            tidyselect_1.1.2   
## [16] compiler_4.1.2      textshaping_0.3.6   cli_3.4.1          
## [19] rvest_1.0.3         xml2_1.3.3          sass_0.4.2         
## [22] classInt_0.4-8      proxy_0.4-27        systemfonts_1.0.4  
## [25] digest_0.6.29       rmarkdown_2.16      pkgconfig_2.0.3    
## [28] htmltools_0.5.3     dbplyr_2.2.1        fastmap_1.1.0      
## [31] highr_0.9           rlang_1.0.6         readxl_1.4.1       
## [34] rstudioapi_0.14     jquerylib_0.1.4     generics_0.1.3     
## [37] farver_2.1.1        jsonlite_1.8.2      googlesheets4_1.0.1
## [40] magrittr_2.0.3      s2_1.1.0            Rcpp_1.0.9         
## [43] munsell_0.5.0       fansi_1.0.3         lifecycle_1.0.2    
## [46] stringi_1.7.8       yaml_2.3.5          MASS_7.3-58.1      
## [49] grid_4.1.2          crayon_1.5.2        lattice_0.20-45    
## [52] haven_2.5.1         hms_1.1.2           knitr_1.40         
## [55] pillar_1.8.1        wk_0.6.0            reprex_2.0.2       
## [58] glue_1.6.2          evaluate_0.16       ggfun_0.0.7        
## [61] modelr_0.1.9        vctrs_0.4.2         tzdb_0.3.0         
## [64] tweenr_2.0.2        cellranger_1.1.0    gtable_0.3.1       
## [67] polyclip_1.10-0     assertthat_0.2.1    cachem_1.0.6       
## [70] xfun_0.33           ggforce_0.4.1       broom_1.0.1        
## [73] e1071_1.7-11        ragg_1.2.3          class_7.3-20       
## [76] googledrive_2.0.0   gargle_1.2.1        units_0.8-0        
## [79] ellipsis_0.3.2