require(maps); require(plotrix); require(rmapshaper); require(sp); require(sf); require(raster); library(nationalparkcolors); # load data d <- read.csv('Data_NonMim_Map.csv', header=T, stringsAsFactors=F); ##plot map m <- map(database='state', region=c('New Mexico', 'Colorado', 'Texas', 'Oklahoma', 'Kansas', 'Missouri'), plot=F, fill=TRUE); map('state', xlim=c(m$range[1:2]), ylim=c(m$range[3:4])); mex <- readRDS('MEX_adm1.rds'); # comment out once loaded so you don't keep reloading it if you run whole code at once # simplify mex mex_simpl <- ms_simplify(mex, keep=0.009); # get rid of border with us m_mex <- st_as_sf(mex_simpl[1]); mex_in <- ms_innerlines(m_mex); # add to state map plot(mex_in, xlim=c(m$range[1:2]), ylim=c(m$range[3:4]), add=T, lwd=1); ##create pies col.fac <- 6; col.vec <- park_palette('GeneralGrant', 8)[4:1]; ##create a color ramp for probability from 0 to 1, with 100 values. Could adjust this to go from prob. of detecting a 5% morph from 1 individual as the lower bound colorProb <- colorRampPalette(c('white', 'black'))(100); for (i in 1:nrow(d)){ ##calculate the probability of finding a rare morph at 5% given your sample size in the population calcProb <- 1- (0.95)^d$Popsize[i]; ##pull that value from the color ramp bgTmp <- colorProb[round(calcProb*100)]; ##plot lines connecting points to pies lines(c(d$Longitude[i], d$PieLong[i]), c(d$Latitude[i], d$PieLat[i]), lwd=0.5); ##plot points with correct background color for % likihood of detection points(d$Longitude[i], d$Latitude[i], cex=1, pch=19, bg=bgTmp, col=bgTmp); ##plot pies with correct colors - making a vector and assigning a non-zero number assures perfect plotting xvec <- NA; for (j in 1:4){ xvec[j] <- d[i,j+col.fac] + 0.001; } if (max(xvec) > 1) { draw.circle(d$PieLong[i], d$PieLat[i], radius = 0.5, border='black', lwd=0.5, col=col.vec[max(xvec)]); } else { floating.pie(xpos=d$PieLong[i], ypos=d$PieLat[i], x=xvec, radius=0.5, col=col.vec, lwd=0.5); } } ##add a scalebar and place it in the Gulf of Mexico scalebar(d=400, xy=c(-94.994125, 28), type='bar', divs=4, below="km", lonlat=T, adj=c(0.5, -1.2), lwd=0.8, cex=1); ##Insert legend ramp for probability of detection text(x=-92.8, y=26.95, 'detection probability', font=1.2, cex=1, xpd=T); # note that this is an odd use of a raster as a quick fix to a color gradient corruption problem at the manuscript proof stage; our original use of gradient.rect() from 'plotrix' did not render correctly in typesetting r <- raster(ncol=10, nrow=10); values(r) <- seq(0, 1, length=100); plot(r, legend.only=TRUE, col=colorProb, legend.width=1, smallplot=c(0.67, 0.85, 0.15, 0.16), axis.args=list(labels=TRUE, at=seq(0, 1.0, by=0.5), pos=2, tick=FALSE), horizontal=TRUE);