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

Graticules in geom_sf #396

Closed
Nowosad opened this issue Jun 23, 2017 · 12 comments
Closed

Graticules in geom_sf #396

Nowosad opened this issue Jun 23, 2017 · 12 comments

Comments

@Nowosad
Copy link
Contributor

Nowosad commented Jun 23, 2017

Graticules works well in the first example - world1 but there are some problems with the second one - world2:

library(tidyverse)                                                              
library(sf)                                                                     
library(maps)                                                                   
                                                                                
world1 <- st_as_sf(map('world', plot = FALSE, fill = TRUE)) %>%                 
st_transform(., "+proj=laea +y_0=0 +lon_0=155 +lat_0=-90 +ellps=WGS84 +no_defs")
                                                                                
ggplot() + geom_sf(data = world1)                                               

                                                                                
                                                                                
world2 <- st_as_sf(map('world', plot = FALSE, fill = TRUE)) %>%                 
st_transform(., "+proj=laea +y_0=0 +lon_0=-77 +lat_0=39 +ellps=WGS84 +no_defs") 
                                                                                
ggplot() + geom_sf(data = world2)                                               

@edzer should I leave it here or post it as the ggplot2 issue?

@edzer
Copy link
Member

edzer commented Jun 25, 2017

Here; it's a problem with st_graticule:

st_graticule() %>% 
  st_transform("+proj=laea +y_0=0 +lon_0=-77 +lat_0=39 +ellps=WGS84 +no_defs") %>% 
  st_geometry %>% 
  plot

x

@edzer
Copy link
Member

edzer commented Jun 25, 2017

Increasing ndiscr in the call to st_graticule helps:

st_graticule(ndiscr=1000) %>% st_transform("+proj=laea +y_0=0 +lon_0=-77 +lat_0=39 +ellps=WGS84 +no_defs") %>% st_geometry %>% plot

x

but how can we pass this such that it happens inside geom_sf? Maybe geom_sf should accept an ndiscr argument? Yes, this needs work on the ggplot2 side.

@edzer
Copy link
Member

edzer commented Jun 25, 2017

With the commit to sf and PR to ggplot2 referenced above, with

ggplot() + geom_sf(data = world2)+ coord_sf(ndiscr=1000)

I now get
x

@edzer edzer removed the help wanted label Jun 25, 2017
@Nowosad
Copy link
Contributor Author

Nowosad commented Jun 25, 2017

Thanks @edzer, great work as always!
I have one related question. Why this argument (ndiscr) is added to coord_sf()? I would, naively, thought that coord_sf() is used to set a CRS and not to change any graphical outputs (such as graticules).

@edzer
Copy link
Member

edzer commented Jun 25, 2017

Other parameters passed to st_graticule (crs, datum) are also passed on to coord_sf, but I don't master the gg logic yet. @hadley, do you think ndiscr should be a parameter to coord_sf, as in this PR, or rather to geom_sf?

@hadley
Copy link
Contributor

hadley commented Jun 26, 2017

There is one set of graticules and they are equivalent to the grid lines in Cartesian coordinations. I think it's pretty clear that the arguments that control them should belong to coord_sf()

@Nowosad Nowosad closed this as completed Jun 27, 2017
hadley pushed a commit to tidyverse/ggplot2 that referenced this issue Jul 3, 2017
* addresses #2119

* tidy graticule fixes

* ggplot2 side of issue r-spatial/sf#396

* tabs -> spaces

* add ndiscr to docs
karawoo pushed a commit to tidyverse/ggplot2 that referenced this issue Jul 28, 2017
* addresses #2119

* tidy graticule fixes

* ggplot2 side of issue r-spatial/sf#396

* tabs -> spaces

* add ndiscr to docs

* fix #2200

* attempt to fix #2060

All cases were in sf.R a geometry column is address with x$geometry, ggplot2 made the wrong assumption that the geometry column has a fixed name. I replaced this in certain instances, where the data are already pretty transformed and no longer have properties of sf objects, with a fixed position, i.e. x[[1]], which seems to work.

* fixes r-spatial/sf#438

* address review comments

* fix break on geom_raster, objects without list-column

see https://gist.github.com/mdsumner/573ec70014df177baa2d1df7e55e1943 for the case that this PR fixes

* tidy up

* trying @karawoo's suggestion

* adds some sf tests

* tidy further
@tsamsonov
Copy link

tsamsonov commented Dec 10, 2017

@edzer To continue a discussion on passing the parameters of st_graticule() to coord_sf(): is there any way to control the lat and lon values for automatically generated graticule in ggplot? Sometimes I am not satisified with the default positions of meridians and parallels, like in this plot, where I want the latitude step to be 10 degrees, not 5:

ggplot_sf

@edzer
Copy link
Member

edzer commented Dec 10, 2017

did you try this?

ggplot() + ... + scale_y_continuous(breaks = c(40,50,60))

@tsamsonov
Copy link

Thanks, it works! Much easier than I expected.

@edzer
Copy link
Member

edzer commented Dec 10, 2017

Same thing here!

@Nowosad Nowosad reopened this Jan 19, 2018
@Nowosad
Copy link
Contributor Author

Nowosad commented Jan 19, 2018

There is another small thing with graticules.
The only difference between figures is that the first one has +lat_0=40 and the second one - +lat_0=39.
However, only the first one has that unusual line of latitude:

library(sf)
#> Linking to GEOS 3.5.0, GDAL 2.1.2, proj.4 4.9.2

st_graticule(ndiscr = 1000) %>% st_transform("+proj=laea +x_0=0 +y_0=0 +lon_0=-74 +lat_0=40 +ellps=WGS84 +no_defs") %>% st_geometry %>% plot

st_graticule(ndiscr = 1000) %>% st_transform("+proj=laea +x_0=0 +y_0=0 +lon_0=-74 +lat_0=39 +ellps=WGS84 +no_defs") %>% st_geometry %>% plot

Created on 2018-01-19 by the reprex
package
(v0.1.1.9000).

@edzer
Copy link
Member

edzer commented Jan 19, 2018

It goes away if you do e.g.

st_graticule(ndiscr = 10000) %>% st_transform("+proj=laea +x_0=0 +y_0=0 +lon_0=-74 +lat_0=40.1 +ellps=WGS84 +no_defs") %>% st_geometry %>% plot

that is, change lat_0 away from 40 and increase ndiscr. (The less you move lat_0, the more you need to increase ndiscr).

I suspect this is not an issue with sf, but rather with approximating graticules with linestrings for particular projections with regular discretization of angles instead of focusing on where we need many points. It's very unlikely that I will work on solving this in the near future, but if someone would bring up a PR doing this I'd be happy to review.

I recall that @hadley pointed me once to a website demonstrating javascript code that did dynamic noding of graticules, but forgot where it was.

@edzer edzer closed this as completed Jan 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants