I am trying to plot some parts of Canada using the ggplot2 package. I know that it could be tricky to keep the entire polygons because ggplot is getting rid of the points outside the limits (if you use limits
in scale_x/y_continuous()
). To overcome this, it is useful to plot the entire map and then to apply a zoom (coord_map). My polygons are all okay except that ocean is in the same color than Canada/Alaska. However, if I look at the entire map it is white.
A working example :
require(rgdal)
require(ggplot2)
YK <- map_data("world") ##World
## Regions Canada
library(autoimage)
data(canada)
library(broom)
canada_df <- tidy(canada)
p <- (ggplot()
+ theme_bw()
+ geom_polygon(data = YK, aes(x=long, y = lat, group = group), fill="gray70")
+ geom_path(data = canada_df, aes(x=long, y=lat, group=group), colour="black"))
p
This gives a correct world map :
Then if I want to zoom at the west part of Canada :
p <- (ggplot()
+ theme_bw()
+ geom_polygon(data = YK, aes(x=long, y = lat, group = group), fill="gray70")
+ geom_path(data = canada_df, aes(x=long, y=lat, group=group), colour="black")
+ coord_map("mercator", xlim=c(-150, -120), ylim=c(50, 70)))
p
This gives :
Regions are correct but the Pacific ocean should be in white in the left side... So I tried with the other way :
p <- (ggplot()
+ theme_bw()
+ geom_polygon(data = YK, aes(x=long, y = lat, group = group), fill="gray70")
+ geom_path(data = canada_df, aes(x=long, y=lat, group=group), colour="black")
+ scale_x_continuous(limits = c(-150, -120))
+ scale_y_continuous(limits = c(50, 70)))
p
Which gives :
The ocean is in white but as excepted, the polygons are cropped...
Is there a way to have this zoom with the entire polygons and the ocean in white ? Because like this, it is as if everything was the continent...
Thank you !
Seems the problem is with coord_map
. If using coord_cartesian
or coord_fixed
the zoom in is ok. And coord_quickmap
works too.
p <- ggplot() +
theme_bw() +
geom_polygon(data = YK, aes(x=long, y = lat, group = group), fill="gray70") +
geom_path(data = canada_df, aes(x=long, y=lat, group=group), colour="black")
p
p + coord_cartesian(xlim=c(-150, -120), ylim=c(50, 70))
p + coord_fixed(ratio = 2, xlim=c(-150, -120), ylim=c(50, 70))
p + coord_quickmap(xlim=c(-150, -120), ylim=c(50, 70))