使用R

时间:2019-01-29 13:05:59

标签: r openair

我目前有一系列的空气质量后向轨迹图,其中是通过R的OpenAir软件包生成的后向轨迹穿越频率的网格。我想在这些图中添加指示着火位置的点。从.csv文件读取的点,其中每个唯一的火都有一个纬度和经度值。

我已经阅读了OpenAir手册,但是没有看到任何解决方案。我试图用以下代码添加点:

Week_One_Freq + points (FireData$longitude, FireData$latitude, col = "red", cex = .6)

但是遇到错误:

Error in Week_One_Freq + points(FireData$longitude, FireData$latitude,:  non-numeric argument to binary operator

我已经测试过,看看问题是否出在火灾数据上。我将要点绘制如下:

library(rworldmap)
newmap <- getMap(resolution = "low")
plot(newmap, xlim = c(-80, 80), ylim = c(-40, 40), asp = 1)
points (FireData$longitude, FireData$latitude, col = "red", cex = .6)

它工作正常,可以正确绘制火场位置。

这是我的频率轨迹图代码:

Week_One_Freq <- trajLevel(Week_1_Traj, statistic = "frequency",
                          smooth = TRUE,
                          map.cols = "grey40", map.alpha = 0.2,
                          projection = "mercator", parameters = NULL,
                          grid.col = "transparent",
                          key.pos = "right", 
                          orientation = c(90,0,0),
                          npoints = 20, origin = TRUE,
                          xlim= c(-80, 80), ylim = c(-40, 40))

到目前为止,我可以在地图上绘制火灾数据点,并且可以为空气质量后移轨迹生成一系列频率图。我想将两者结合起来,以便在一张地图上既有后退轨迹,又有指示火灾地点的点。

编辑:用于创建频率图的轨迹数据的示例:

structure(list(X = 1:5, Receptor = c(1L, 1L, 1L, 1L, 
1L, 1L), Year = c(2017L, 2017L, 2017L, 2017L, 2017L, 2017L), 
Month = c(10L, 10L, 10L, 10L, 10L, 10L), Day = c(6L, 5L, 
5L, 5L, 5L, 5L), Hour = c(0L, 23L, 22L, 21L, 20L, 19L), hour.inc = 0:-5, 
lat = c(-3.162, -3.189, -2.937, -2.87, -2.809, -2.753), lon = c(-60.6, 
-60.439, -60.282, -60.13, -60.986, -60.851), height = c(500, 
502.2, 500.6, 495.2, 486.4, 474.3), pressure = c(944.2, 942.9, 
942.4, 942.3, 942.7, 943.5), date = structure(c(1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("06/10/2017 00:00", "06/10/2017 03:00", 
"06/10/2017 06:00", "06/10/2017 09:00", "06/10/2017 12:00", 
"06/10/2017 15:00", "06/10/2017 18:00", "06/10/2017 21:00", 
"07/10/2017 00:00", "07/10/2017 03:00", "07/10/2017 06:00", 
"07/10/2017 09:00", "07/10/2017 12:00", "07/10/2017 15:00", 
"07/10/2017 18:00", "07/10/2017 21:00", "08/10/2017 00:00", 
"08/10/2017 03:00", "08/10/2017 06:00", "08/10/2017 09:00", 
"08/10/2017 12:00", "08/10/2017 15:00", "08/10/2017 18:00", 
"08/10/2017 21:00", "09/10/2017 00:00", "09/10/2017 03:00", 
"09/10/2017 06:00", "09/10/2017 09:00", "09/10/2017 12:00", 
"09/10/2017 15:00", "09/10/2017 18:00", "09/10/2017 21:00", 
"10/10/2017 00:00", "10/10/2017 03:00", "10/10/2017 06:00", 
"10/10/2017 09:00", "10/10/2017 12:00", "10/10/2017 15:00", 
"10/10/2017 18:00", "10/10/2017 21:00", "11/10/2017 00:00", 
"11/10/2017 03:00", "11/10/2017 06:00", "11/10/2017 09:00", 
"11/10/2017 12:00", "11/10/2017 15:00", "11/10/2017 18:00", 
"11/10/2017 21:00", "12/10/2017 00:00", "12/10/2017 03:00", 
"12/10/2017 06:00", "12/10/2017 09:00", "12/10/2017 12:00", 
"12/10/2017 15:00", "12/10/2017 18:00", "12/10/2017 21:00"
), class = "factor"), Date = structure(c(1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("06/10/2017", "07/10/2017", "08/10/2017", 
"09/10/2017", "10/10/2017", "11/10/2017", "12/10/2017"), class = 
"factor")), row.names = c(NA, 
6L), class = "data.frame")

火灾数据示例:

structure(list(acq_date = structure(c(6L, 6L, 6L, 6L, 6L, 6L), .Label = 
c("01/11/2017", "02/11/2017", "03/11/2017", "04/11/2017", "05/11/2017", 
"06/10/2017", "06/11/2017", "07/10/2017", "08/10/2017", "09/10/2017", 
"10/10/2017", "11/10/2017", "12/10/2017", "13/10/2017", "14/10/2017", 
"15/10/2017", "16/10/2017", "17/10/2017", "18/10/2017", "19/10/2017", 
"20/10/2017", "21/10/2017", "22/10/2017", "23/10/2017", "24/10/2017", 
"25/10/2017", "26/10/2017", "27/10/2017", "28/10/2017", "29/10/2017", 
"30/10/2017", "31/10/2017"), class = "factor"), latitude = c(-0.18499, 
-0.18777, -0.18832, -0.1932, -0.19654, -0.21077), longitude = c(-48.89484, 
-48.89885, -48.89536, -49.06121, -49.06173, -48.97051)), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

有人在https://github.com/davidcarslaw/openair/issues/68解决了相同的问题。您可以使用gridExtra中的layer函数将点添加到最后一个网格对象,但是首先需要使用mapproject将点放置到正确的投影上。我已经扩展了他们的示例,以包括一些其他信息,例如所需的库。

library(openair)
library(maps)
library(mapproj)
library(latticeExtra)

# import your data
Trajectory_Data <- read.table("Analysis.txt")
Fire_Data <- read.csv("Fire_Data.csv")

lon <- Fire_Data$longitude  
lat <- Fire_Data$latitude  
#convert points to projected lat/long
tmp <- mapproject(x=lon, y=lat, projection='lambert', parameters = c(51, 51), orientation = c(90, 0, 0))  
# I found the projection, parameters, and orientation by looking at the default values for arguments in the trajLevel function 

# Plot trajectories
trajLevel(subset(Trajectory_Data, lon > -70 & lon < -20 & lat > -15 & lat < 15),
          pollutant = "BC", statistic = "cwt", smooth = TRUE, col = "increment", border = NA)
# add the points. you can adjust the size of the points with cex
c <- trellis.last.object() + layer(lpoints(tmp$x, tmp$y, pch=16, cex=0.05, col="black"))
print(c)