I'm plotting some points on a map of the world using the R maps
package, something like:
The command to draw the base map is:
map("world", fill=TRUE, col="white", bg="gray", ylim=c(-60, 90), mar=c(0,0,0,0))
But I need to display Pacific centred map. I use map("world2",
etc to use the Pacific centred basemap from the maps package, and convert the coordinates of the data points in my dataframe (df
) with:
df$longitude[df$longitude < 0] = df$longitude[df$longitude < 0] + 360
This works if I don't use the fill
option, but with fill
the polygons which cross 0° cause problems.
I guess I need to transform the polygon data from the maps
library somehow to sort this out, but I have no idea how to get at this.
My ideal solution would be to draw a maps with a left boundary at -20° and a right boundary at -30° (i.e. 330°). The following gets the correct points and coastlines onto the map, but the crossing-zero problem is the same
df$longitude[df$longitude < -20] = df$longitude[d$longitude < -20] + 360
map("world", fill=TRUE, col="white", bg="gray", mar=c(0,0,0,0),
ylim=c(-60, 90), xlim=c(-20, 330))
map("world2", add=TRUE, col="white", bg="gray", fill=TRUE, xlim=c(180, 330))
Any help would be greatly appreciated.
map_data
function inggplot2
to put the data intodata.frame
format, and then check each group to see if it is closed, and add a new data point if not. – Alanisrecenter
fromsp
andst_shift_longitude
fromsf
packages now do this. – Polyandrist