简体   繁体   English

XY中的XYZ数据到2D图

[英]XYZ data to 2D plot in R

I need to make a 2D plot of distance travelled versus my value at that point ("intensity"). 我需要制作一个距离行程的2D曲线图与该点的值(“强度”)。

My data is formatted as: 我的数据格式为:

lon lat intensity

 1. -85.01478 37.99030  -68.3167
 2. -85.00752 37.97601  -68.0247
 3. -85.00027 37.96172  -67.9565
 4. -84.99302 37.94743  -67.8917

and it continues for 282 rows like this. 它继续像这样282行。 I was looking at a few packages that calculate distance between longitude (lon) and latitude (lat) points (such as geosphere), but I couldn't understand how to get my data into the format that it wanted. 我正在查看一些计算经度(lon)和纬度(lat)点(例如geosphere)之间距离的软件包,但我无法理解如何将我的数据转换为它想要的格式。 I know the total distance travelled in degrees should be 4.01538, evenly spaced out between the 282 points, but I don't know how I could make a column in R with this in mind. 我知道以度为单位的总行进距离应该是4.01538,在282点之间均匀分布,但我不知道如何在R中制作一个列。

dfrm$dist<- cumsum(c(0, with(dfrm, sqrt( (lat[-1]-lat[-nrow(dfrm)])^2+
                                   (lon[-1]-lon[-nrow(dfrm)])^2
                               )))  )
with(dfrm, plot(dist, intensity, type="b"))

在此输入图像描述

Or choose a more "geographic" distance measure with the lagged column values. 或者使用滞后列值选择更“地理”的距离度量。 But given the increments, I doubt the error from using a naive distance measure can be that much. 但考虑到增量,我怀疑使用天真距离测量的误差可能是那么多。

From here I found some packages to calculate distance between coordinates. 这里我发现了一些包来计算坐标之间的距离。 Assuming your data is called dtf and using the RSEIS package: 假设您的数据被称为dtf并使用RSEIS包:

dtf <- data.frame(rbind(c(-85.01478,37.99030,-68.3167),
c(-85.00752,37.97601,-68.0247),c(-85.00027,37.96172,-67.9565),
c(-84.99302,37.94743,-67.8917)))
names(dtf) <- c('lon','lat','int')

library(RSEIS)
travelint <- function(i,data){
ddeg <- GreatDist(dtf$lon[i],dtf$lat[i],dtf$lon[i+1],dtf$lat[i+1])$ddeg;
dint <- dtf$int[i+1] - dtf$int[i]; return(list(ddeg,dint))}
out <- sapply(1:(nrow(dtf)-1),data=dtf,travelint)
out <- data.frame(matrix(as.numeric(out),ncol=2,byrow=T))
out$X1 <- cumsum(out$X1)

This will take your data, calculate the distance traveled between points and the intensity change between them. 这将获取您的数据,计算点之间的行进距离以及它们之间的强度变化。 After that it can be plotted like this: 之后可以像这样绘制:

ggplot(out,aes(X1,X2)) + geom_line() + 
      labs(x="Distance (Degrees)",y="Intensity Change")

在此输入图像描述

If instead you want increasing intensity , you can use cumsum again to get the cumulative change in intensity and then add it to the first intensity: 如果你想要增加强度,你可以再次使用cumsum来获得强度的累积变化,然后将其添加到第一个强度:

out2 <- out
out2 <- rbind(c(0,0),out2)
out2$X2 <- cumsum(out2$X2) + dtf$int[1]
ggplot(out2,aes(X1,X2)) + geom_line() + 
      labs(x="Distance (Degrees)",y="Intensity")

在此输入图像描述

As mentioned by DWin you can use naive measure or geographic distance measure. 如DWin所述,您可以使用天真测量或地理距离测量。 Here I am using gdist function from Imap package calculates Great-circle distance . 这里我使用来自Imap包的gdist函数计算大圆距离。

library(Imap)
library(lattice)
#Dummy data
longlat <- read.table(text="lon lat intensity
 1. -85.01478 37.99030  -68.3167
 2. -85.00752 37.97601  -68.0247
 3. -85.00027 37.96172  -67.9565
 4. -84.99302 37.94743  -67.8917", header=TRUE)

ll <- lapply(seq(nrow(longlat)-1), function(x){
    start <- longlat[x,]
    end   <- longlat[x+1,]
    cbind(distance = gdist(start$lon, start$lat, end$lon, end$lat,units = "m"),
          intensity = end$intensity - start$intensity)
  })
dd <- as.data.frame(do.call(rbind,ll))
library(lattice)
xyplot(intensity~distance,dd,type= c('p','l'),pch=20,cex=2)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM