I tried to make several bubble graphs showing the frequency of observation (in percent) of several individuals in different places. Some people were found on one site, but not all. In addition, the number of places within each site may vary between people. My main problem is that I have more than 3 people and more than 3 sites, so I try to create a good / fast way to create this type of bubbles / legends. I also have problems with the legend, since I need to have a function that will put the legend in the same place when creating a new plot. In legend, I want to show different bubble sizes for each frequency (if possible, indicating a value next to the bubble).
Here is an example of my script. Any suggestions or ideas on how to do this would be extremely helpful.
# require libraries library(maptools) library(sp) data<-read.table(text="ind lat long site freq perc A -18.62303 147.29207 A 449 9.148329258 A -18.6195 147.29492 A 725 14.77180114 A -18.62512 147.3018 A 3589 73.12550937 A -18.62953 147.29422 A 145 2.954360228 B -18.75383 147.25405 B 2 0.364963504 B -18.73393 147.28162 B 1 0.182481752 B -18.62303 147.29207 A 3 0.547445255 B -18.6195 147.29492 A 78 14.23357664 B -18.62512 147.3018 A 451 82.29927007 B -18.62953 147.29422 A 13 2.372262774 C -18.51862 147.39717 C 179 0.863857922 C -18.53281 147.39052 C 20505 98.95757927 C -18.52847 147.40167 C 37 0.178562811",header=TRUE) # Split data frame for each tag ind<-data$ind M<-split(data,ind) l<-length(M) ### Detection Plots ### pdf("Plots.pdf",width=11,height=8,paper="a4r") par(mfrow=c(1,1)) for(j in 1:l){ # locations new.data<-M[[j]] site<-as.character(unique(new.data$site)) fname<-paste(new.data$ind[1],sep="") loc<-new.data[,c("long","lat")] names(loc)<-c("X", "Y") coord<-SpatialPoints(loc) coord1<-SpatialPointsDataFrame(coord,new.data) # draw some circles with specify radius size x<-new.data$long y<-new.data$lat freq<-new.data$perc rad<-freq rad1<-round(rad,1) title<-paste("Ind","-",fname," / ","Site","-",new.data$site[1],sep="") # create bubble plot symbols(x,y,circles=rad1,inches=0.4,fg="black",bg="red",xlab="",ylab="") points(x,y,pch=1,col="black",cex=0.4) par(new=T) # map scale maps::map.scale(grconvertX(0.4,"npc"),grconvertY(0.1, "npc"), ratio=FALSE,relwidth=0.2,cex=0.6) # specifying coordinates for legend legX<-grconvertX(0.8,"npc") legY1<-grconvertY(0.9,"npc") legY2<-legY1-0.001 legY3<-legY2-0.0006 legY4<-legY3-0.0003 # creating the legend leg<-data.frame(X=c(legX,legX,legX,legX),Y=c(legY1,legY2,legY3,legY4), rad=c(1000,500,100,25)) symbols(leg$X,leg$Y,circles=leg$rad,inches=0.3,add=TRUE,fg="black",bg="white") mtext(title,3,line=1,cex=1.2) mtext("Latitude",2,line=3,padj=1,cex=1) mtext("Longitude",1,line=2.5,padj=0,cex=1) box() } dev.off()
The first graph is actually good, and he will need to have a frequency / perc value next to the angengine bubble. However, this does not work with others ...