Export from Access (survey 1) a table with columns FULL_NAME, TRC, SUC, QUC, FRCOV, call it “MOCY1.txt”
Export from Access (survey 2) a table with columns FULL_NAME, TRC, SUC, QUC, FRCOV, call it “MOCY2.txt”
Load those files into R and combine them:
#load data
frequency1<-read.table("~/Dropbox/p/gloria/MOCY1.txt",sep=",",colClasses=c("character","factor","factor","factor","character"))
frequency2<-read.table("~/Dropbox/p/gloria/MOCY2.txt",sep=",",colClasses=c("character","factor","factor","factor","character"))
#add names if necessary (my .txt exports didn't have column names)
names(frequency1)<-c("FULL_NAME","TRC","SUC","QUC","FRCOV")
names(frequency2)<-c("FULL_NAME","TRC","SUC","QUC","FRCOV")
#combine surveys (can also combine various TRCs at this point if you like)
frequency1$MOCY<-1
frequency2$MOCY<-2
frequency<-rbind(frequency1,frequency2)
frequency$MOCY<-as.factor(frequency$MOCY)
require(ggplot2)
require(dplyr)
require(reshape2)
Expand FRCOV from a 100 length character string into 100 binary columns
expanded<-c()
for(each.row in 1:dim(frequency)[1]){
string<-frequency[each.row,5]
list.of.presences<-t(sapply(strsplit(string,""),as.numeric))
expanded<-rbind(expanded,list.of.presences)
}
expanded <- data.frame(expanded)
names(expanded)<-paste("cell",rep(c(1,2,3,4,5,6,7,8,9,0),each=10),rep(c(1,2,3,4,5,6,7,8,9,0)))
expanded <- data.frame(frequency,expanded)
Turn this wide data into tidy, “long” data (very long - this will add 100 rows for each combination of quadrat & species)
long<-melt(expanded)
#turn the "cell name" column into coordinates
long$y<-as.numeric(substr(long$variable,6,6))
long$x<-as.numeric(substr(long$variable,8,8))
long[long$y==0,"y"]<-10
long[long$x==0,"x"]<-10
#add unique quadrat code
long$quadrat=paste(long$TRC,long$SUC,long$QUC)
Custom code to do comparison plot in one quadrat
comparison.plot<-function(quad,data){
QUCdata<-subset(data, quadrat==quad&value==1)
ggplot(QUCdata, aes(x, y)) +
geom_text(aes(label = MOCY),alpha=1/2) +
scale_x_continuous("",breaks=c(1:10))+
scale_y_reverse("",breaks=c(1:10))+
facet_wrap(~ FULL_NAME) +
theme_bw()+
labs(title=quad)+
theme(panel.grid.major = element_blank())+
theme(aspect.ratio = 1) # try with and without
}
comparison.plot("DXS SYI E33",long) #example data
In this example we can see that we’ve got to carefully check the photos/data - it seems likely that the Rhododendron identification must be wrong in one year or another.
If you want to do it to all of the plots and export them as PDFs for printing or veiwing on the computer for QC or other reasons:
print_all_comparison.plots<-function(data){
for(each_quadrat in unique(data$quadrat)[1:2]){
print(each_quadrat) #gives feedback - on older computers this can take a while
p<-comparison.plot(each_quadrat,data)
ggsave(filename=paste0("test",each_quadrat,".pdf"),width=8.5,height=11)
print("saved") #gives feedback
}
}