setwd("~/Desktop/Mediterranean_project/publications/paper_Italy/Palmisano_etal_data_and_code")
library(rcarbon)
library(doParallel)
library(rgdal)
library(raster)
library(maptools)
library(maps)
library(gtools)
library(sf)
## Load data
mydates <- read.csv("csv/dates.csv", header=TRUE, stringsAsFactors=FALSE, encoding="UTF-8",na.strings=c("NA",""),strip.white=TRUE)
# Load study area polygon (from GIS-friendly, ESRI-format shapefiles  to "SpatialPolygonsDataFrame" objects in R)
ita<-readOGR("shp/italy_regions.shp", layer="italy_regions")
north<-ita[ita$ID==1,]
central<-ita[ita$ID==2,]
south<-ita[ita$ID==3,]
sicily<-ita[ita$ID==4,]
sardinia<-ita[ita$ID==5,]
# Turn the whole date dataset into a spatial object than can be plotted on a map
# First, make sure the coordinates are treated as numbers
mydates$Longitude <- as.numeric(mydates$Longitude)
mydates$Latitude <- as.numeric(mydates$Latitude)
# Then throw out any without coordinates
datesp <- mydates[!is.na(mydates$Longitude) & !is.na(mydates$Latitude),]
# Then designate the coordinate columns
coordinates(datesp) <- ~Longitude+Latitude
# Then stipulate that the coordinate system of this data is the same as the polygon data we have (i.e. unprojected latlon)
proj4string(datesp) <- proj4string(north)<-proj4string(central)<-proj4string(south)<-proj4string(sardinia)<-proj4string(sicily)
#Spatial query: selection of points (datesp) located within region (=1)
query_north<-over(datesp,north)
query_central<-over(datesp,central)
query_south<-over(datesp,south)
query_sicily<-over(datesp,sicily)
query_sardinia<-over(datesp,sardinia)
#convert vector into dataframe
north<-as.data.frame(datesp)
central<-as.data.frame(datesp)
south<-as.data.frame(datesp)
sicily<-as.data.frame(datesp)
sardinia<-as.data.frame(datesp)
#add column for storing spatial query's results
north["query"]<-NA
central["query"]<-NA
south["query"]<-NA
sicily["query"]<-NA
sardinia["query"]<-NA
#update the column "query"
north$query<-query_north
central$query<-query_central
south$query<-query_south
sicily$query<-query_sicily
sardinia$query<-query_sardinia
#subsetting the dates located in the study area
mydates_north<-subset(north, north$query==1)
mydates_central<-subset(central, central$query==2)
mydates_south<-subset(south, south$query==3)
mydates_sicily<-subset(sicily, sicily$query==4)
mydates_sardinia<-subset(sardinia, sardinia$query==5)
#add column for defining region
mydates_north["Region"]<-1
mydates_central["Region"]<-2
mydates_south["Region"]<-3
mydates_sicily["Region"]<-4
mydates_sardinia["Region"]<-5
#aggregate all Radiocarbon dates
mydates<-rbind(mydates_north,mydates_central,mydates_south,mydates_sardinia, mydates_sicily)
#select radiocarbon dates from short-lived samples
shortlived<-mydates[grep(paste(c("antler","bone","seed","seeds", "tissue","fruit", "bome", "tooth","horn", "twig","grain", "collagen", "hair", "leather"),collapse="|"), mydates$Material),] # subselect shortlived samples
# Turn the whole date dataset into a spatial object than can be plotted on a map
# First, make sure the coordinates are treated as numbers
mydates$Longitude <- as.numeric(mydates$Longitude)
mydates$Latitude <- as.numeric(mydates$Latitude)
mydates_north$Longitude <- as.numeric(mydates_north$Longitude)
mydates_north$Latitude <- as.numeric(mydates_north$Latitude)
mydates_central$Longitude <- as.numeric(mydates_central$Longitude)
mydates_central$Latitude <- as.numeric(mydates_central$Latitude)
mydates_south$Longitude <- as.numeric(mydates_south$Longitude)
mydates_south$Latitude <- as.numeric(mydates_south$Latitude)
mydates_sicily$Longitude <- as.numeric(mydates_sicily$Longitude)
mydates_sicily$Latitude <- as.numeric(mydates_sicily$Latitude)
mydates_sardinia$Longitude <- as.numeric(mydates_sardinia$Longitude)
mydates_sardinia$Latitude <- as.numeric(mydates_sardinia$Latitude)
# Then throw out any without coordinates
datesp<-mydates[!is.na(mydates$Longitude) & !is.na(mydates$Latitude),]
datesp_north <- mydates_north[!is.na(mydates_north$Longitude) & !is.na(mydates_north$Latitude),]
datesp_central <- mydates_central[!is.na(mydates_central$Longitude) & !is.na(mydates_central$Latitude),]
datesp_south <- mydates_south[!is.na(mydates_south$Longitude) & !is.na(mydates_south$Latitude),]
datesp_sicily <- mydates_sicily[!is.na(mydates_sicily$Longitude) & !is.na(mydates_sicily$Latitude),]
datesp_sardinia <- mydates_sardinia[!is.na(mydates_sardinia$Longitude) & !is.na(mydates_sardinia$Latitude),]
# Then designate the coordinate columns
coordinates(datesp) <- ~Longitude+Latitude
lonlat <- CRS("+init=epsg:4326") # LatLon WGS84
proj4string(datesp) <- lonlat
countries<-readOGR("shp/eur_states_lo.shp", layer="eur_states_lo")
datesp <- spTransform(datesp, proj4string(countries)) # UTM WGS84, zone 37 North
coordinates(datesp_north) <- ~Longitude+Latitude
coordinates(datesp_central) <- ~Longitude+Latitude
coordinates(datesp_south) <- ~Longitude+Latitude
coordinates(datesp_sicily) <- ~Longitude+Latitude
coordinates(datesp_sardinia) <- ~Longitude+Latitude
# Then stipulate that the coordinate system of this data is the same as the polygon data we have (i.e. unprojected latlon)
proj4string(datesp_north) <- proj4string(datesp_central)<-proj4string(datesp_south)<-proj4string(datesp_sardinia)<-proj4string(datesp_sicily)
nsim <- 1000 # number of actual simulations
ncores <- 6 # multi-core processing (set higher if available)
runm <- 50 #smoothing of SPDs
binh <- 50 #bin clustering
realstartBP <- 10000
realendBP <-2500
bracket <- 1000
workingstartBP <- realstartBP+bracket
workingendBP <- realendBP-bracket
if (workingendBP<0){ workingendBP <- 0 }
alldates <- calibrate(x=mydates$CRA, errors=mydates$Error, calCurves='intcal20', method="standard", normalised=FALSE, ncores=ncores, calMatrix=TRUE)
shortlivedates <- calibrate(x=shortlived$CRA, errors=shortlived$Error, calCurves='intcal20', method="standard", normalised=FALSE, ncores=ncores, calMatrix=TRUE)
bins <- binPrep(sites=mydates$SiteID, ages=mydates$CRA, h=binh)
allspd<- spd(x=alldates, bins=bins, timeRange=c(workingstartBP,workingendBP), datenormalised=FALSE, runm=runm)
bins_shortlived <- binPrep(sites=shortlived$SiteID, ages=shortlived$CRA, h=binh)
allspd_shortlived <- spd(x=shortlivedates, bins=bins_shortlived, timeRange=c(workingstartBP,workingendBP), datenormalised=FALSE, runm=runm)
plot(allsp)
plot(allspd)
plot(allspd_shortlived)
plot(allspd)
plot(allspd_shortlived, type="fill", col="darkgreen", add=T)
plot(allspd_shortlived, type="standard", col="darkgreen", add=T)
plot(allspd_shortlived, type="standard", fill.p="darkgreen", add=T)
jpeg("rplot.jpg")
dev.off()
jpeg(file="rplot.jpg")
dev.off()
jpeg('rplot.jpg')
dev.off()
dev.print(device = jpg, file="SPD.jpg")
dev.print(device = jpeg, file="SPD.jpg")
dev.print(device = jpeg, file="SPD.jpg", width=600, height=300)
dev.print(device = jpeg, file="SPD.jpg", width=1200, height=800)
dev.print(device = pdf, file="SPD.pdf")
View(alldates)
View(alldates)
View(alldates)
mydates <- read.csv("csv/dates.csv", header=TRUE, stringsAsFactors=FALSE, encoding="UTF-8",na.strings=c("NA",""),strip.white=TRUE)
View(mydates)
3085/40.1
1056/40.1
3103/40.1
