R - how to get a smooth curve between two points?
-
13-03-2021 - |
Domanda
I'm trying to draw a line between two cities, on a world map, using R (using map and wrld_simpl).
How can I get a smooth line between the two points? If I drew a line between US and Australia, it has three different segments
require(rworldmap)
map("world", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05)
data(wrld_simpl)
US_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'United States']
US_lon = wrld_simpl$LON[wrld_simpl$NAME == 'United States']
australia_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'Australia']
australia_lon = wrld_simpl$LON[wrld_simpl$NAME == 'Australia']
lines(c(US_lon, US_lat), c(australia_lon, australia_lat))
This draws a line, but it isn't correctly between aus and US. what am I doing wrong?
Soluzione
The method lines
takes a vector x
of x-coordinates and y
of y-coordinates. Therefore your line should look like this:
lines(c(US_lon, australia_lon), c(US_lat, australia_lat))
If you want a great circle, the following will work:
require(geosphere)
gc <- gcIntermediate(c(US_lon, US_lat), c(australia_lon, australia_lat), breakAt=TRUE, n=1000)
invisible(lapply(gc, lines, col='red', lwd=2))
Altri suggerimenti
You can also draw a "curve" using grid.curve():
grid.curve(US_lon, australia_lon, US_lat, australia_lat)
... and you can define the "look" of the curve.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow