map projections and coord_map and world maps

392 views
Skip to first unread message

Stephen

unread,
May 10, 2012, 7:22:01 PM5/10/12
to ggp...@googlegroups.com

The documentation for v0.9.0 says about Map projections, “This is still experimental, and if you have any advice to offer regarding a better (or more correct) way to do this, please let me know.” While not exactly advice, I believe I’ve identified some bugs in coord_map and borders. My question is whether I’m using borders and coord_map incorrectly, or whether borders and coord_map are truly flawed and can be fixed? Below I outline the problems and provide code to produce illustrative examples.

First, artifacts appear on maps when using borders and coord_map. These reflect errors in the polygons that are being created.

library(ggplot2)

library(maps)

p <- ggplot()

p <- p + borders("world", fill = 1)

p   # no artifacts

p + coord_map() #artifacts


These artifacts are worsened when x and y limits are set:

p + coord_map(xlim=c(0,180), ylim=c(0,90))

If xlim and ylim are specified separately, there are fewer errors.

p + coord_map() + xlim(0,180) + ylim(0,90)


All these artifacts are absent if one employs the map function of the maps package to plot the same data, though this precludes using ggplot2 to make complex map overlays, which is my goal.

map(database="world", xlim=c(0,180), ylim=c(0,90))


Second, there is a workaround, using geom_path (rather than borders) to draw the map lines and specifying x and y limits separately from coord_map.

p <- ggplot()

wrld <- map_data("world")

p <- p + geom_path(data=wrld, aes(x=long, y=lat, group = group))

p # no artifacts


#artifacts return using coord_maps x and y limits

p + coord_map(xlim=c(0,180), ylim=c(0,90))


# no  artifacts if x and y limits specified separately

p + coord_map() + xlim(0,180) + ylim(0,90)


However, this workaround precludes the shading of landmasses (since they’re now paths and not polygons). It also precludes some of the map-specific functionality of borders and map_coord, such as interchangeably specifying longitudes with the range -180 to 180 degrees or 0 to 360 degrees; this particular capability is key for a map spanning the antimeridian.

p <- ggplot()

p <- p + borders("world")

p

# specify xlim within coord_map with range 0 to 360 degrees, albeit with artifacts

p + coord_map(xlim=c(100,300), ylim=c(0,90))

# cannot specify xlim separately with range 0 to 360

p + coord_map() + xlim(100,300) + ylim(0,90) 


p <- ggplot()

wrld <- map_data("world")

p <- p + geom_path(data=wrld, aes(x=long, y=lat, group = group))

p

# cannot specify xlim separately with range 0 to 360

p + coord_map() + xlim(100, 300) + ylim(0,90)


Again, there is a workaround; one can use the map data “world2”, which has longitudes in the range 0 to 360. Although this also requires that any data to be plotted in another layer also have longitudes in the range 0 to 360 rather than the customary range of -180 to 180. The necessary conversion can be accomplished by adding 360 to longitude values less than 0.

p <- ggplot()

wrld <- map_data("world2")

p <- p + geom_path(data=wrld, aes(x=long, y=lat, group = group))

p # lots of artifacts

# can specify separate xlim using 0 to 360; artifacts now gone

p + coord_map() + xlim(180, 225) + ylim(50,75)


# though artifacts may still appear wth particular limits

p + coord_map() + xlim(0, 360) + ylim(0,85)

p + coord_map() + xlim(1, 360) + ylim(0,85)

p + coord_map() + xlim(1, 359) + ylim(0,85)


Again, my overall question is whether borders and coord_map can be fixed so borders and polygons of the world map are drawn correctly?

Regards,

Stephen


> sessionInfo()

R version 2.14.2 (2012-02-29)

Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)


locale:

[1] C/en_US.UTF-8/C/C/C/C

attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:

[1] mapproj_1.1-8.3 maps_2.2-5      ggplot2_0.9.1  

loaded via a namespace (and not attached):

 [1] MASS_7.3-18        RColorBrewer_1.0-5 colorspace_1.1-1   dichromat_1.2-4   

 [5] digest_0.5.2       grid_2.14.2        labeling_0.1       memoise_0.1       

 [9] munsell_0.3        plyr_1.7.1         proto_0.3-9.2      reshape2_1.2.1    

[13] scales_0.2.1       stringr_0.6        tools_2.14.2

Reply all
Reply to author
Forward
0 new messages