Plotting specified part of world map

64 views
Skip to first unread message

bulva

unread,
Nov 12, 2010, 9:38:56 AM11/12/10
to ggplot2
Hello.
Say I would want to plot a part of globe specified by limiting
(rectangular) longitude and latitude limits. Seems that whatever geom
I used so far the plot isn't what I expect it to be - paths or
polygons are not plotted till the end of box...

library(ggplot2)
world.map <- map_data("world")

max_lat = 16.5
min_lat = 2.0
max_lon = 10.5
min_lon = -20.0

p.map <- ggplot(data=world.map)
p.map <- p.map + geom_path(aes(long, lat, group = group))
p.map <- p.map +xlim(min_lon, max_lon) + ylim(min_lat, max_lat)
p.map <- p.map + coord_map(projection = "tetra")
p.map <- p.map + opts(panel.background = theme_rect(fill =
"lightblue", colour="white"))
print(p.map)

Andrie de Vries

unread,
Nov 12, 2010, 12:48:26 PM11/12/10
to ggplot2
bulva

The trick here is that you need to pass the limits of your map to the
coordinate system, and not to the scales. xlim and ylim are shortcuts
to modify the extent of the scales, and all data outside the scale
range is removed from the dataframe before plotting.

You need to pass xlim and ylim to to coord_map() as in the following
code. Note that takes a very long time to process, so you would
probably want to reduce the dataset by using the map tools to specify
the region of the world map that you are actually looking for.


library(ggplot2)
library(maps)

world.map <- map_data("world")

max_lat = 16.5
min_lat = 2.0
max_lon = 10.5
min_lon = -20.0

p.map <- ggplot(data=world.map) +
geom_path(aes(long, lat, group = group)) +
coord_map(projection = "tetra", xlim=c(min_lon, max_lon),
ylim=c(min_lat, max_lat)) +
opts(panel.background = theme_rect(fill = "lightblue",
colour="white"))


print(p.map)

bulva

unread,
Nov 12, 2010, 2:24:03 PM11/12/10
to ggplot2
Thank You for the swift answer, that indeed does it :) I'll work on
selecting the relevant part of data and then feeding it to ggplot.
Reply all
Reply to author
Forward
0 new messages