【问题标题】:Use a for loop to create several bubble plots with different legend scales in R使用 for 循环在 R 中创建多个具有不同图例比例的气泡图
【发布时间】:2013-04-03 12:16:19
【问题描述】:

我一直在尝试制作几个气泡图,显示不同地点几个人的观察频率(以百分比表示)。在同一地点发现了一些人,但不是全部。此外,每个站点内的位置数量可能因人而异。我的主要问题是我有超过 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()

第一个图实际上没问题,只需要在图例气泡旁边显示频率/perc 的值。但是,它并不能真正与其他人一起使用...

【问题讨论】:

    标签: r function for-loop plot legend


    【解决方案1】:

    您正在对图例位置进行硬编码 - 使其相对...

    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% 为步长)。

    【讨论】:

    • 用特定类别(75)将气泡大小(在绘图和图例中)表示为百分比的好方法是什么? %)?现在我的数据是汇总的,所以每一行都有 %observations 的值,但我也有每天所有观察值(频率)的原始数据。有没有一种好方法可以为多个人在绘图上计算这种间隔?
    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 2019-03-07
    • 2023-03-12
    • 2019-07-05
    • 2021-09-20
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多