chm
chm

Reputation: 41

How to plot a specific region of a map?

I want to plot a specific part of Germany (German Bight). Therefore I used followig code

library(ggplot2)
library(mapdata)

lon_min <- 5
lon_max <- 10
lat_min <- 52
lat_max <- 57

coast_map <- fortify(map("worldHires", 
                     xlim = c(lon_min, lon_max), 
                     ylim = c(lat_min, lat_max), 
                     fill = T, plot = F))

ggplot(coast_map, aes(x = long, y = lat, group = group)) +
geom_polygon()

And this is the picture I get map of germany

Well, this isn't really what I want to get. I don't want to plot hole Germany but a specific region of it. As you can see above, I definied longitude and latitude.

 lon_min <- 5
 lon_max <- 10
 lat_min <- 52
 lat_max <- 57

Why the entire map of Germany is shown to me? Is there a way to cut the map at corresponding coordinates while using geom_polygon()? Thanks.

Upvotes: 2

Views: 1815

Answers (2)

Eric Fail
Eric Fail

Reputation: 7948

Is this what you are looking for? (code below)

ggplot2 German Bight

# install.packages("mapdata", dependencies = TRUE)
# install.packages("ggplot2", dependencies = TRUE)

library(ggplot2)
library(mapdata)

lon_min <- 5
lon_max <- 10
lat_min <- 52
lat_max <- 57

coast_map <- fortify(map("worldHires", 
                     xlim = c(lon_min, lon_max), 
                     ylim = c(lat_min, lat_max), 
                     fill = T, plot = F))

ggplot(coast_map, aes(x = long, y = lat, group = group)) + geom_polygon() + 
       coord_map(xlim=c(lon_min, lon_max), ylim=c(lat_min, lat_max))

Upvotes: 1

lukeA
lukeA

Reputation: 54257

You could add + coord_map(xlim=c(lon_min, lon_max), ylim=c(lat_min, lat_max)).

Upvotes: 0

Related Questions