rm(list=ls()); library("RColorBrewer") # Load data SonPops <- read.csv('Sonora_struc_data_CODES_morphs.csv', header=T, stringsAsFactors=F); # 1: set up plot dimensions col.vec <- c("#FFEDA0", "#FEB24C"); # colors for structure demes quartz.options(height=5, width=10); par(oma=c(0,0,0.5,0)); par(mar=c(2,2,3,0)); par(mfrow=c(2,1)) plot.new(); pop.cex <- 0.85; # set pop label size for easy changes ##################### Mimetic Morphs ########################## ##U = uniform; RS = red-striped; B = banded; M = mimetic## # 2: Sort by morph popOrderDD <- c('B', 'RS', 'M', 'U', 'Unknown'); SonPops$MimMorph <- factor(SonPops$MimMorph, levels=popOrderDD); # sort dataframe for mim morps d1 <- SonPops[order(SonPops$MimMorph, SonPops$Population), ]; # check to make sure it worked (will just print pop and morph to screen) d1[,c(1,13)]; # set plot window for this frame plot.window(xlim=c(0, nrow(d1)), ylim=c(0, 1)); # 3: loop over all rows in your dataframe and plot each polygon in order for (i in 1:nrow(d1)){ polygon(x=c(i-1, i-1, i, i), y=c(0, d1$PropClust1[i], d1$PropClust1[i], 0), col=col.vec[1], border=NA); polygon(x=c(i-1, i-1, i, i), y=c(d1$PropClust1[i], 1, 1, d1$PropClust1[i]), col=col.vec[2], border=NA); } # 4: overlay your black lines for pop boundaries, as well as population labels t <- table(d1$MimMorph)[popOrderDD]; counter <- 0; # assign pop label text colors by state pop.col <- c('#355C7D', '#F67280', '#009900', '#6666FF', '#994C00', 'black'); states <- c('TX', 'NM', 'OK', 'MO', 'KS', 'CO'); names(pop.col) <- states; state.vec <- d1$State; exp.col <- pop.col[state.vec]; text.vec <- c('Crossbanded', 'Red-Striped', 'Mimetic', 'Uniform', 'Unknown'); names(text.vec) <- c('B', 'RS', 'M', 'U', 'Unknown'); # add color lines underneath for (i in 1:nrow(d1)){ lines(x=c(i-1, i), y=c(-0.02, -0.02), col=exp.col[i], lwd=3, xpd=T); } # add label text and morph breaks for (i in 1:(length(t)-1)){ counter1 <- counter + t[i]; text(x=(counter+((counter1-counter)/2)), y=1.05 , labels=text.vec[names(text.vec) == names(t[i])], cex=0.8, xpd=T); lines(x=c(counter1, counter1), y=c(-0.05, 1.05), lwd=0.75); counter <- counter1; } # 5: add custom axes and labels par(mgp=c(1,0.6,0)); # scoots the labels closer to the ticks! axis(2, at=seq(0,1, by=0.5), las=1, line=-1.2, cex.axis=0.9); mtext(text="Populations Ordered by Mimetic Morph", side=1, line=0.75, cex=1); mtext(text="Assignment Probability", side=2, line=0.7, cex=1); mtext(text="A", side=3, line=2, cex=1.3, font=2, at=-8); # add custom "unknown" label arrows(x0=108.5, x1=108.5, y0=1.03, y1=1.10, angle=30, length=0.04, xpd=T, col='black'); text(x=108.5, y=1.17, labels='Unknown', cex=0.8, xpd=T); # add outer margin legend legend('top', legend=c('Texas', 'New Mexico', 'Oklahoma', 'Missouri', 'Kansas', 'Colorado'), col=pop.col, lwd=2, xpd=T, bty='n', horiz=T, text.font=3, cex=0.9, inset=c(0, -0.45), x.intersp=0.5); # note that states are listed in order of appearance from left to right in panel A (rather than alphabetical, etc.) ##################### Non-Mimetic Morph ########################## frame(); # 2: Sort by non-mim morph, custom order popOrderDD <- c('BC', 'NB', 'B', 'N', 'Unknown'); SonPops$NonMimMorph_lack <- factor(SonPops$NonMimMorph_lack, levels=popOrderDD); dl <- SonPops[order(SonPops$NonMimMorph_lack, SonPops$Population), ]; # check to make sure it worked (will just print pop and morph to screen) dl[,c(1,14)]; # re-set plot window for this new frame plot.window(xlim=c(0, nrow(dl)), ylim=c(0, 1)); # 3: loop over all rows in your dataframe and plot each polygon in order for (i in 1:nrow(dl)){ polygon(x=c(i-1, i-1, i, i), y=c(0, dl$PropClust1[i], dl$PropClust1[i], 0), col=col.vec[1], border=NA); polygon(x=c(i-1, i-1, i, i), y=c(dl$PropClust1[i], 1, 1, dl$PropClust1[i]), col=col.vec[2], border=NA); } # 4: overlay your black lines for pop boundaries, as well as population labels t <- table(d1$NonMimMorph_lack)[popOrderDD]; counter <- 0; # assign pop label text colors by state state.vec <- dl$State; exp.col <- pop.col[state.vec]; text.vec <- c("Black Cap", NA, 'Both', "Neither", 'Unknown'); names(text.vec) <- c('BC', 'NB', 'B', 'N', 'Unknown'); for (i in 1:nrow(dl)){ lines(x=c(i-1, i), y=c(-0.02, -0.02), col=exp.col[i], lwd=3, xpd=T); } for (i in 1:(length(t)-1)){ counter1 <- counter + t[i]; text(x=(counter+((counter1-counter)/2)), y=1.05 , labels=text.vec[names(text.vec) == names(t[i])], cex=0.8, xpd=T); lines(x=c(counter1, counter1), y=c(-0.05, 1.05), lwd=0.75); counter <- counter1; } # 5: add custom axes and labels axis(2, at=seq(0,1, by=0.5), las=1, line=-1.2, cex.axis=0.9); mtext(text="Populations Ordered by Non-Mimetic Morph", side=1, line=0.75, cex=1); mtext(text="Assignment Probability", side=2, line=0.7, cex=1); mtext(text="B", side=3, line=2, cex=1.3, font=2, at=-8); # add custom "Collar" and "unknown" labels arrows(x0=108, x1=108, y0=1.03, y1=1.10, angle=30, length=0.04, xpd=T, col='black'); text(x=108, y=1.17, labels='Unknown', cex=0.8, xpd=T); arrows(x0=46, x1=46, y0=1.03, y1=1.10, angle=30, length=0.04, xpd=T, col='black'); text(x=46, y=1.17, labels='Nuchal Band', cex=0.8, xpd=T);