# Load required packages
library(ggplot2)
library(dplyr)
library(sf)
library(scatterpie)
library(mapdata)
library(tidyr)

# read data
points_gibellula <- read.delim("D:/Downloads/QMUL & RBG Kew/aaa. MSc Course Content/BIO709P - P&F Project/Specimen & Sample Inventory/Figures/Distribution map/2_Thairine Mendes gibellula pul_leio_alata_location.txt", sep = "\t")

# combine reference with gibellula spp. 
points_gibellula <- points_gibellula %>%
  mutate(
    Gibellula.species = factor(paste0(Gibellula.species, " (", Reference, ")"))
  ) 

# Convert the data frame to sf format
sf_data <- st_as_sf(points_gibellula, coords = c("Country.long", "Country.lat")) 

# Set the CRS to WGS 84 (EPSG:4326)
sf_data <- st_set_crs(sf_data, 4326)

# Group the points by region and species and calculate presence/absence
pie_chart_data <- sf_data %>%
  group_by(Country = paste(geometry)) %>%
  summarise(Presence = as.integer(n() > 0))

# Create a summary of unique Country.long and Country.lat values for each region
summary_data <- sf_data %>%
  group_by(Country = paste(geometry)) %>%
  summarize(
    Unique_Countries = n_distinct(Gibellula.species),
    Total_Species = n()
  )

# Merge the summary data with the pie chart data using st_join
pie_chart_data <- st_join(pie_chart_data, summary_data)


# Calculate the percentage of each species in each region
pie_chart_data <- pie_chart_data %>%
  group_by(Country) %>%
  mutate(Percentage = Presence / Total_Species * 100)
Error in `st_as_sf()`:
! Must group by variables found in `.data`.
✖ Column `Country` is not found.
Run `rlang::last_trace()` to see where the error occurred.

gibellula pul_leio_alata_location txt file used for R script

0

There are 0 best solutions below