[R-sig-Geo] Positing a coord_proj for ggplot2

Roger Bivand Roger.Bivand at nhh.no
Sun Jul 26 17:31:02 CEST 2015

On Fri, 24 Jul 2015, boB Rudis wrote:

> Hey folks
> Having finally gotten tired of performing coordinate conversions to
> Winkel-Tripel (and other) projections prior to passing map data to
> ggplot2 (coord_map projection support is limited to what mapproject
> supports and that's pretty scant) I was toying with a coord_proj
> coordinate system for ggplot2 the other day and came up with this:
> https://gist.github.com/hrbrmstr/363e33f74e2972c93ca7 (visual:
> http://rpubs.com/hrbrmstr/coord-proj).
> coord_proj uses proj4::project, so it supports any proj4string.

Please use proj4::ptransform to add support for datum transformations, 
which are often needed. Also note that the help page for the function is 
probably incorrect, as the default +ellps= in PROJ.4 if given is WGS84.

> This won't be in ggplot2 right now as Hadley is updating/refactoring
> the package and "coords" are going to work a bit differently. Plus
> this adds a dependency I'm not sure he wants to add to it. You can
> stick it in your own, local copy of ggplot2 and re-collate and rebuild
> to use it.
> proj4::project (and even spTransform, to a certain degree - no pun
> intended) are unhappy dealing with lat/lon values outside the -90/90 :
> -180:180 range and even have some issues with points right at the
> extremes, so I did the following in the linked code:
> df$x <- ifelse(df$x <= -180, -179.999999999, df$x)
> df$x <- ifelse(df$x >= 180, 179.999999999, df$x)
> df$y <- ifelse(df$y <= -90, -89.999999999, df$y)
> df$y <- ifelse(df$y >= 90, 89.999999999, df$y)

This may well be unsafe, as 0 - 360 is also valid for longitudes, and in 
many cases -180 - 360 will also be feasible. The test in sp is in 
sp:::.ll_sanity() which uses an internal tolerance (which may be set) to 
do similar checks.


> It was a quick hack to just get coord_proj up and running and I know
> it's a horrible one, but I'm looking for guidance/feedback/etc on what
> I _should_ do here since I will be maintaining some version of this
> moving forward (it's just too convenient for me not to and the new
> "coords" may be as extensible as the forthcoming change to "geoms", so
> this could be in a package all itself or added to another R geo
> package).
> thx,
> -Bob
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no

More information about the R-sig-Geo mailing list