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

geom_hline() / geom_vline() don't work with geom_sf() #2820

Closed
clauswilke opened this issue Aug 10, 2018 · 2 comments
Closed

geom_hline() / geom_vline() don't work with geom_sf() #2820

clauswilke opened this issue Aug 10, 2018 · 2 comments

Comments

@clauswilke
Copy link
Member

Reprex below. Primarily reporting this to show the PR I'm about to make didn't mess this up. It's already broken. :-)

library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs

# works
ggplot(nc) +
  geom_sf() + 
  geom_line(
    data = data.frame(x = c(-80, -80), y = c(34, 36.5)),
    aes(x, y),
    color = "red"
  )

# doesn't work
ggplot(nc) +
  geom_sf() + 
  geom_hline(yintercept = 35)
#> Error in unit(x0, default.units): 'x' and 'units' must have length > 0

# doesn't work
ggplot(nc) +
  geom_sf() + 
  geom_vline(xintercept = -80)
#> Error in unit(y0, default.units): 'x' and 'units' must have length > 0

Created on 2018-08-10 by the reprex package (v0.2.0).

@clauswilke
Copy link
Member Author

The cause of this problem is that coord_sf() doesn't implement its own range() function but names the ranges in the panel parameters x_range and y_range instead of x.range and y.range, so the default range function fails and returns NULL. I think a fix should be folded into #2821.

library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"))
#> Reading layer `nc' from data source `/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs

p <- ggplot(nc) + geom_sf()
b <- ggplot_build(p)
names(b$layout$panel_params[[1]])
#> [1] "x_range"   "y_range"   "graticule" "crs"
b$layout$coord$range(b$layout$panel_params[[1]])
#> $x
#> NULL
#> 
#> $y
#> NULL

p <- ggplot(data.frame(x = 1, y = 1), aes(x, y)) + geom_point()
b <- ggplot_build(p)
names(b$layout$panel_params[[1]])
#>  [1] "x.range"        "x.labels"       "x.major"        "x.minor"       
#>  [5] "x.major_source" "x.minor_source" "x.arrange"      "y.range"       
#>  [9] "y.labels"       "y.major"        "y.minor"        "y.major_source"
#> [13] "y.minor_source" "y.arrange"
b$layout$coord$range(b$layout$panel_params[[1]])
#> $x
#> [1] 0.5 1.5
#> 
#> $y
#> [1] 0.5 1.5

Created on 2018-08-10 by the reprex package (v0.2.0).

clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue Aug 10, 2018
clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue Aug 11, 2018
clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue Aug 17, 2018
clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue Aug 22, 2018
clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue Aug 23, 2018
@lock
Copy link

lock bot commented Feb 19, 2019

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Feb 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant