简体   繁体   中英

XYZ data to 2D plot in R

I need to make a 2D plot of distance travelled versus my value at that point ("intensity").

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. 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. 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.

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 <- 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:

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. Here I am using gdist function from Imap package calculates Great-circle distance .

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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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