Beto Mohr
Beto Mohr

Reputation: 443

How to plot a variable with geographic distance in R?

I would like to plot a simple xy graphic being y=variable and x=geographic distance.

I have data.frame with my data of interest in separate columns (ex: Species$Latitude, Species&Longitude, Species$Variable). All coordinates are in decimal degrees and all variable values are numeric.

Something like the attached image. enter image description here Can someone help me? I think it it's easy, but I'm having a hard time figuring it out (so not so easy actually).

Upvotes: 1

Views: 185

Answers (1)

CIAndrews
CIAndrews

Reputation: 1053

When you have a point of origin, you can use the haversine formule to calculate the distance: Haversine function in R

Update, added sample code:

library(pracma)
names <- c("lion","tiger","flamengo")
latitude <- c(0,3,-5)
longitude <- c(0,-0.5,2)
species <- data.frame(names, latitude, longitude)
for(i in 1:length(species$latitude)){
  loc1 <- c(0,0)
  loc2 <- c(species$latitude[i],species$longitude[i])
  species$distance[i] <- haversine(loc1, loc2)
}
species

Upvotes: 2

Related Questions