簡體   English   中英

使用for循環在R中創建多個具有不同圖例比例的氣泡圖

[英]Use a for loop to create several bubble plots with different legend scales in R

我一直在嘗試制作幾個氣泡圖,以顯示不同地點的幾個人的觀察頻率(以百分比表示)。 在同一站點中發現了一些人,但不是全部。 同樣,每個站點內的位置數量可能因人而異。 我的主要問題是我有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() 

第一個圖實際上是確定的,只需要在Lendend氣泡旁邊輸入頻率/ perc的值即可。 但是,它實際上不能與其他人一起使用...

您正在對圖例位置進行硬編碼-使其相對...

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%為步長)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM