在 R 中自动生成大圆地图
我在流动数据 大圆绘图教程并将它们与评论中链接的代码结合起来,以防止R绘制跨赤道大圆时发生奇怪的事情。这给了我这样的想法:
airports <- read.csv("/home/geoff/Desktop/DissertationData/airports.csv", header=TRUE)
flights <- read.csv("/home/geoff/Desktop/DissertationData/ATL.csv", header=TRUE, as.is=TRUE)
library(maps)
library(geosphere)
checkDateLine <- function(l){
n<-0
k<-length(l)
k<-k-1
for (j in 1:k){
n[j] <- l[j+1] - l[j]
}
n <- abs(n)
m<-max(n, rm.na=TRUE)
ifelse(m > 30, TRUE, FALSE)
}
clean.Inter <- function(p1, p2, n, addStartEnd){
inter <- gcIntermediate(p1, p2, n=n, addStartEnd=addStartEnd)
if (checkDateLine(inter[,1])){
m1 <- midPoint(p1, p2)
m1[,1] <- (m1[,1]+180)%%360 - 180
a1 <- antipode(m1)
l1 <- gcIntermediate(p1, a1, n=n, addStartEnd=addStartEnd)
l2 <- gcIntermediate(a1, p2, n=n, addStartEnd=addStartEnd)
l3 <- rbind(l1, l2)
l3
}
else{
inter
}
}
# Unique months
monthyear <- unique(flights$month)
# Color
pal <- colorRampPalette(c("#FFEA00", "#FF0043"))
colors <- pal(100)
for (i in 1:length(monthyear)) {
png(paste("monthyear", monthyear[i], ".png", sep=""), width=750, height=500)
map("world", col="#191919", fill=TRUE, bg="black", lwd=0.05)
fsub <- flights[flights$month == monthyear[i],]
fsub <- fsub[order(fsub$cnt),]
maxcnt <- max(fsub$cnt)
for (j in 1:length(fsub$month)) {
air1 <- airports[airports$iata == fsub[j,]$airport1,]
air2 <- airports[airports$iata == fsub[j,]$airport2,]
p1 <- c(air1[1,]$long, air1[1,]$lat)
p2 <- c(air2[1,]$long, air2[1,]$lat)
inter <- clean.Inter(p1,p2,n=100, addStartEnd=TRUE)
colindex <- round( (fsub[j,]$cnt / maxcnt) * length(colors) )
lines(inter, col=colors[colindex], lwd=1.0)
}
dev.off()
}
我想自动为包含所有预定商业路线的大型数据集制作地图 - 虚拟样本 —在 ATL 和全球网络中的其他机场之间共享(airports.csv 在流动数据帖子中链接)。我最好每月制作一张地图,将其用作描述亚特兰大机场网络空间变化的短视频的框架。
问题:每次运行循环时,我都无法让循环生成多个 PNG(仅从每个 CSV 中的第一个唯一月份)。我相当肯定 Aaron Hardin 的代码“破坏”了流动数据教程中使用的自动化。经过三天的摆弄并寻找任何相关的 R 操作方法之后,我意识到我只是缺乏协调其中一个与另一个的能力。有人可以帮助我自动化该过程吗?
里面有一份论文致谢词给你!
I've taken some of the things I learned in a Flowing Data great circle mapping tutorial and combined them with code linked in the comments to prevent weird things from happening when R plots trans-equatorial great circles. That gives me this:
airports <- read.csv("/home/geoff/Desktop/DissertationData/airports.csv", header=TRUE)
flights <- read.csv("/home/geoff/Desktop/DissertationData/ATL.csv", header=TRUE, as.is=TRUE)
library(maps)
library(geosphere)
checkDateLine <- function(l){
n<-0
k<-length(l)
k<-k-1
for (j in 1:k){
n[j] <- l[j+1] - l[j]
}
n <- abs(n)
m<-max(n, rm.na=TRUE)
ifelse(m > 30, TRUE, FALSE)
}
clean.Inter <- function(p1, p2, n, addStartEnd){
inter <- gcIntermediate(p1, p2, n=n, addStartEnd=addStartEnd)
if (checkDateLine(inter[,1])){
m1 <- midPoint(p1, p2)
m1[,1] <- (m1[,1]+180)%%360 - 180
a1 <- antipode(m1)
l1 <- gcIntermediate(p1, a1, n=n, addStartEnd=addStartEnd)
l2 <- gcIntermediate(a1, p2, n=n, addStartEnd=addStartEnd)
l3 <- rbind(l1, l2)
l3
}
else{
inter
}
}
# Unique months
monthyear <- unique(flights$month)
# Color
pal <- colorRampPalette(c("#FFEA00", "#FF0043"))
colors <- pal(100)
for (i in 1:length(monthyear)) {
png(paste("monthyear", monthyear[i], ".png", sep=""), width=750, height=500)
map("world", col="#191919", fill=TRUE, bg="black", lwd=0.05)
fsub <- flights[flights$month == monthyear[i],]
fsub <- fsub[order(fsub$cnt),]
maxcnt <- max(fsub$cnt)
for (j in 1:length(fsub$month)) {
air1 <- airports[airports$iata == fsub[j,]$airport1,]
air2 <- airports[airports$iata == fsub[j,]$airport2,]
p1 <- c(air1[1,]$long, air1[1,]$lat)
p2 <- c(air2[1,]$long, air2[1,]$lat)
inter <- clean.Inter(p1,p2,n=100, addStartEnd=TRUE)
colindex <- round( (fsub[j,]$cnt / maxcnt) * length(colors) )
lines(inter, col=colors[colindex], lwd=1.0)
}
dev.off()
}
I'd like to automate the production of maps for a large dataset containing all scheduled commercial routes — dummy sample — shared between ATL and other airports in the global network (airports.csv is linked to in the Flowing Data post). Preferably, I'd produce one map per month that I would use as frame in a short video depicting changes in the Atlanta airport network space.
The problem: I can't get the loop to produce any more than one PNG—from only the first unique month in each CSV—each time I run it. I'm fairly certain Aaron Hardin's code 'breaks' the automation as it is used in the Flowing Data tutorial. After three days of messing with it and chasing down any relevant R how-to's, I realize I simply lack the chops to reconcile one with the other. Can anybody help me automate the process?
There's a dissertation acknowledgement in it for you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论的信息太多,所以我改为发布答案。这是我的想法(并阅读到最后以了解可能存在的问题):
我尝试在流动数据教程中的原始数据上运行您的代码。 (显然你必须为每月数据添加一列,所以我只是添加了这一行来随机化月份:):
每当我有一个需要很长时间运行的循环时,我通常会在其中粘贴一些代码进度检查。使用您喜欢的内容:
print
、cat
、tcltk::tkProgressBar
。我使用message
:无论如何,我然后运行了你的代码。一切都按其应有的方式进行。由于我采样了四个月的数据,我得到:
png
图,每个图都有一个黑暗的世界地图和亮黄色的线条。这是四行之一:那么,为什么它在我的机器上运行而不是在你的机器上运行?
我只能猜测,但我的猜测是你还没有设置工作目录。您的代码中没有
setwd
,对png
的调用只是给出文件名。我怀疑您的代码正在写入系统中的工作目录中。默认情况下,在我的安装中,工作目录为:
要解决此问题,请执行以下操作之一:
setwd()
在脚本顶部设置工作目录。png()
时使用完整路径和文件名Too much information for a comment, so I post an answer instead. Here is what I think (and read to the end to see what could potentially be the problem):
I have tried to run your code on the original data in the Flowing Data tutorial. (Obviously you have to add a column for monthly data, so I simply added this line to randomise the month:):
Whenever I have a loop that takes a long time to run, I generally stick a bit of code in there that gives me a progress check. Use what takes your fancy:
print
,cat
,tcltk::tkProgressBar
. I usemessage
:Anyway, I then ran your code. Everything works exactly as it should. Since I sampled four months worth of data, I get:
png
plots, each with a dark world map and bright yellow lines. Here is one of the four lines:So, why does it work on my machine and not yours?
I can only guess, but my guess is that you haven't set the working directory. There is no
setwd
in your code, and the call topng
just gives the filename. I suspect your code is being written to whatever your working directory is in your system.By default, on my installation, the working directory is:
To solve this, do one of the following:
setwd()
to set your working directory at the top of your script.png()