使用for循环在R中创建具有不同图例比例的多个气泡图

时间:2013-04-11 10:28:13

标签: r function for-loop plot legend

我一直试图制作几个气泡图,显示不同地点的几个人的观察频率(百分比)。有些人被发现在同一个网站,但不是全部。此外,每个站点内的位置数量可能因人而异。我的主要问题是我有超过3个人和3个以上的网站,所以我一直试图想出一个好的/快速的方法来创建这种类型的泡沫图/传说。我也遇到了传奇问题,因为我需要一个能够在创建新图时将图例放在同一位置的功能。在图例中,我想为每个频率显示不同的气泡大小(如果可能,请指示气泡旁边的值)。

以下是我的脚本示例。有关如何执行此操作的任何建议或想法都将非常有用。

# 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()

第一个图实际上是Ok,并且只需要在lengend bubble旁边有频率/ perc的值。但是,它并没有真正与其他人合作......

1 个答案:

答案 0 :(得分:2)

你正在硬编码传奇的位置 - 让它相对......

legX<-grconvertX(0.8,"npc")
legY1<-grconvertY(0.9,"npc")

# Get the size of the plotting area (measured on the y axis)
ysize <- par()$usr[4]-par()$usr[3]

# Use that to calculate the new positions
legY2<-legY1 - (0.1* ysize)
legY3<-legY1 - (0.2* ysize)
legY4<-legY1 - (0.3* ysize)

这会将气泡放在所有图上的相同位置(以绘图区域的10%为步长)。