Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proj4 error (probably the coord munching around curvature) #21

Open
mdsumner opened this issue Aug 15, 2016 · 3 comments
Open

proj4 error (probably the coord munching around curvature) #21

mdsumner opened this issue Aug 15, 2016 · 3 comments
Assignees
Labels

Comments

@mdsumner
Copy link

mdsumner commented Aug 15, 2016

Sorry to post this without a solution, but I have to leave it for now.

I get an error from proj4::project for (seemingly) identical inputs when used in coord_proj(), but not directly.

It's likely to do with the coord_munching which I haven't explored yet - these polygons are both stretched around the edges of the laea map with these parameters, and that would likely be true for different polygons and projection combos. (It might be true for the old mapproj idioms for ggplot, but I never learnt to use them).

I don't know if this is a munching problem for ggalt or ggplot2, but either way I'm happy to help. A general "topojson"-like solution would be good in R, etc. etc.

Preparation.

library(dplyr)

library(maptools)
data(wrld_simpl)
library(ggalt)
## either 24 or 175 triggers the problem, everything else is fine
ff <- fortify(wrld_simpl[24, ])
proj <- "+proj=laea +lat_0=-90 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
  1. Ggplot not ok.
ggplot(ff) + aes(x = long, y = lat, group = group) + geom_polygon() + 
  coord_proj(proj, inverse = FALSE, degrees = TRUE, 
             ellps.default = NA)
Error in proj4::project(list(x = df$x, y = df$y), proj = coord$proj, inverse = coord$inverse,  : 
                          tolerance condition error
  1. Cannot recreate error with direct call
df <- ff %>% mutate(x = long, y = lat)

## this shouldn't affect, seems not to
df$x <- ifelse(df$x <= -180, -179.99999999999, df$x)
df$x <- ifelse(df$x >= 180, 179.99999999999, df$x)
df$y <- ifelse(df$y <= -90, -89.99999999999, df$y)
df$y <- ifelse(df$y >= 90, 89.99999999999, df$y)

plot(proj4::project(list(x=df$long, y=df$lat),
                    proj = proj,
                    inverse = FALSE,
                    degrees  = TRUE, 
                    ellps.default = NA), pch = ".", asp = 1)
## how I found the offending objects 24 and 175
# for (i in seq_along(wrld_simpl$NAME)) {
# ff <- fortify(subset(wrld_simpl, NAME == wrld_simpl$NAME[i]))
# proj <- "+proj=laea +lat_0=-90 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
# 
# ## 
# print(
# ggplot(ff) + aes(x = long, y = lat, group = group) + geom_polygon() + 
#   coord_proj(proj, inverse = FALSE, degrees = TRUE, 
#              ellps.default = NA)
# )
# }
@hrbrmstr hrbrmstr added the bug label Aug 15, 2016
@hrbrmstr hrbrmstr self-assigned this Aug 15, 2016
@mdsumner
Copy link
Author

This works fine now in the new regime:

## new regime (github master branches 2017-02-15)
library(sf)
library(ggplot2)

ggplot(st_as_sf(wrld_simpl[24, ])) + geom_sf() + coord_sf(crs = proj)

ggplot(st_transform(st_as_sf(wrld_simpl[24, ]), proj)) + geom_sf() 

@hrbrmstr
Copy link
Owner

aye. gave a shout out to the sf ecosystem in the ggalt 0.4.0 blog announcement (shld be hitting rbloggers in the AM). Hope to retire coord_proj() soon ;-)

@mdsumner
Copy link
Author

oh, but what about non-sf data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants