Skip to content

stat_sf(geom = "text") #2111

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

Closed
cpsievert opened this issue Apr 25, 2017 · 4 comments
Closed

stat_sf(geom = "text") #2111

cpsievert opened this issue Apr 25, 2017 · 4 comments
Labels
feature a feature request or enhancement layers 📈

Comments

@cpsievert
Copy link
Contributor

I'd expect something like this to work

library(ggplot2)
library(sf)
library(albersusa)
usa_sf <- st_as_sf(usa_composite("laea"))
centroids <- st_centroid(usa_sf)

ggplot() + 
  geom_sf(data = usa_sf) + 
  stat_sf(data = centroids, aes(geometry = geometry, label = name), geom = "text")
@hadley hadley added feature a feature request or enhancement layers 📈 labels Jun 13, 2017
@hadley
Copy link
Member

hadley commented Oct 31, 2017

@edzer is there an obvious way to do this with st_as_grob()? I don't think there is.

@edzer
Copy link
Contributor

edzer commented Nov 1, 2017

No, as sf:::st_as_grob.POINT simply calls grid::pointGrob. We could modify it such that it takes a label argument and then calls grid::textGrob; alternatively you could get the coordinates (as 2-column matrix, native units) in ggplot by

> head(st_coordinates(centroids))
           X            Y
1 -1071416.4 -1116201.262
2   690503.2 -1091192.221
3 -1707964.5  -667016.439
4  -479407.9  -650634.740
5  2215964.8    -1222.643
6  1960178.5  -403145.999

@hadley
Copy link
Member

hadley commented Nov 1, 2017

I had a go at implementing this but ultimately it seemed like it was going to overload geom_sf() with too many options. I think for this case it's probably better to extract x and y into their own columns and use geom_text() directly.

@hadley hadley closed this as completed Nov 1, 2017
@tiernanmartin
Copy link

tiernanmartin commented Nov 30, 2017

For those following the breadcrumbs, here's one way of implementing @hadley's recommendation (warning: even with ggrepel the labels aren't pretty but that's a separate issue):

library(tidyverse) # devtools::install_github('tidyverse/tidyverse')
library(sf)
#;-) Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
library(albersusa) # devtools::install_github('hrbrmstr/albersusa')
library(ggrepel)

usa_sf <-
  st_as_sf(usa_composite("laea")) %>%
  mutate(
    CENTROID = map(geometry, st_centroid),
    COORDS = map(CENTROID, st_coordinates),
    COORDS_X = map_dbl(COORDS, 1),
    COORDS_Y = map_dbl(COORDS, 2)
  ) %>%
  as_tibble() %>%
  st_as_sf()

ggplot(data = usa_sf) +
  geom_sf( color = "white") +
  geom_text_repel(mapping = aes(COORDS_X, COORDS_Y, label = name), size = 3, min.segment.length = 0) +
  coord_sf(crs = st_crs(usa_sf), datum = NA) + # styling - not necessary
  theme_void() # styling - not necessary

Session info
devtools::session_info()
#;-) Session info -------------------------------------------------------------
#;-)  setting  value                       
#;-)  version  R version 3.4.2 (2017-09-28)
#;-)  system   x86_64, mingw32             
#;-)  ui       RTerm                       
#;-)  language (EN)                        
#;-)  collate  English_United States.1252  
#;-)  tz       America/Los_Angeles         
#;-)  date     2017-11-30
#;-) Packages -----------------------------------------------------------------
#;-)  package    * version    date       source                             
#;-)  albersusa  * 0.3.0      2017-11-30 Github (hrbrmstr/albersusa@82220d3)
#;-)  assertthat   0.2.0      2017-04-11 CRAN (R 3.4.1)                     
#;-)  backports    1.1.1      2017-09-25 CRAN (R 3.4.1)                     
#;-)  base       * 3.4.2      2017-09-28 local                              
#;-)  bindr        0.1        2016-11-13 CRAN (R 3.4.1)                     
#;-)  bindrcpp   * 0.2        2017-06-17 CRAN (R 3.4.1)                     
#;-)  bitops       1.0-6      2013-08-17 CRAN (R 3.4.1)                     
#;-)  broom        0.4.2      2017-02-13 CRAN (R 3.4.0)                     
#;-)  cellranger   1.1.0      2016-07-27 CRAN (R 3.4.1)                     
#;-)  class        7.3-14     2015-08-30 CRAN (R 3.4.2)                     
#;-)  classInt     0.1-24     2017-04-16 CRAN (R 3.4.2)                     
#;-)  cli          1.0.0      2017-11-05 CRAN (R 3.4.2)                     
#;-)  colorspace   1.3-2      2016-12-14 CRAN (R 3.4.1)                     
#;-)  compiler     3.4.2      2017-09-28 local                              
#;-)  crayon       1.3.4      2017-11-16 Github (r-lib/crayon@b5221ab)      
#;-)  datasets   * 3.4.2      2017-09-28 local                              
#;-)  DBI          0.7        2017-06-18 CRAN (R 3.4.1)                     
#;-)  devtools     1.13.4     2017-11-09 CRAN (R 3.4.2)                     
#;-)  digest       0.6.12     2017-01-27 CRAN (R 3.4.1)                     
#;-)  dplyr      * 0.7.4      2017-09-28 CRAN (R 3.4.2)                     
#;-)  e1071        1.6-8      2017-02-02 CRAN (R 3.4.2)                     
#;-)  evaluate     0.10.1     2017-06-24 CRAN (R 3.4.2)                     
#;-)  forcats    * 0.2.0      2017-01-23 CRAN (R 3.4.1)                     
#;-)  foreign      0.8-69     2017-06-22 CRAN (R 3.4.2)                     
#;-)  ggplot2    * 2.2.1.9000 2017-11-30 Github (tidyverse/ggplot2@505e4bf) 
#;-)  ggrepel    * 0.7.0      2017-11-16 Github (slowkow/ggrepel@680db17)   
#;-)  glue         1.2.0      2017-10-29 CRAN (R 3.4.2)                     
#;-)  graphics   * 3.4.2      2017-09-28 local                              
#;-)  grDevices  * 3.4.2      2017-09-28 local                              
#;-)  grid         3.4.2      2017-09-28 local                              
#;-)  gtable       0.2.0      2016-02-26 CRAN (R 3.4.1)                     
#;-)  haven        1.1.0      2017-07-09 CRAN (R 3.4.1)                     
#;-)  hms          0.3        2016-11-22 CRAN (R 3.4.1)                     
#;-)  htmltools    0.3.6      2017-04-28 CRAN (R 3.4.1)                     
#;-)  httr         1.3.1      2017-08-20 CRAN (R 3.4.1)                     
#;-)  jsonlite     1.5        2017-06-01 CRAN (R 3.4.1)                     
#;-)  knitr        1.17       2017-08-10 CRAN (R 3.4.2)                     
#;-)  lattice      0.20-35    2017-03-25 CRAN (R 3.4.2)                     
#;-)  lazyeval     0.2.1      2017-10-29 CRAN (R 3.4.2)                     
#;-)  lubridate    1.7.1      2017-11-03 CRAN (R 3.4.2)                     
#;-)  magrittr     1.5        2014-11-22 CRAN (R 3.4.1)                     
#;-)  maptools     0.9-2      2017-03-25 CRAN (R 3.4.2)                     
#;-)  memoise      1.1.0      2017-04-21 CRAN (R 3.4.2)                     
#;-)  methods    * 3.4.2      2017-09-28 local                              
#;-)  mnormt       1.5-5      2016-10-15 CRAN (R 3.4.1)                     
#;-)  modelr       0.1.1      2017-07-24 CRAN (R 3.4.1)                     
#;-)  munsell      0.4.3      2016-02-13 CRAN (R 3.4.1)                     
#;-)  nlme         3.1-131    2017-02-06 CRAN (R 3.4.2)                     
#;-)  parallel     3.4.2      2017-09-28 local                              
#;-)  pkgconfig    2.0.1      2017-03-21 CRAN (R 3.4.1)                     
#;-)  plyr         1.8.4      2016-06-08 CRAN (R 3.4.1)                     
#;-)  psych        1.7.8      2017-09-09 CRAN (R 3.4.2)                     
#;-)  purrr      * 0.2.4      2017-10-18 CRAN (R 3.4.2)                     
#;-)  R6           2.2.2      2017-06-17 CRAN (R 3.4.1)                     
#;-)  Rcpp         0.12.14    2017-11-23 CRAN (R 3.4.2)                     
#;-)  RCurl        1.95-4.8   2016-03-01 CRAN (R 3.4.1)                     
#;-)  readr      * 1.1.1      2017-05-16 CRAN (R 3.4.1)                     
#;-)  readxl       1.0.0      2017-04-18 CRAN (R 3.4.1)                     
#;-)  reshape2     1.4.2      2016-10-22 CRAN (R 3.4.1)                     
#;-)  rgdal        1.2-16     2017-11-21 CRAN (R 3.4.2)                     
#;-)  rgeos        0.3-26     2017-10-31 CRAN (R 3.4.2)                     
#;-)  rlang        0.1.4      2017-11-05 CRAN (R 3.4.2)                     
#;-)  rmarkdown    1.8        2017-11-17 CRAN (R 3.4.2)                     
#;-)  rprojroot    1.2        2017-01-16 CRAN (R 3.4.2)                     
#;-)  rvest        0.3.2      2016-06-17 CRAN (R 3.4.1)                     
#;-)  scales       0.5.0.9000 2017-11-30 Github (hadley/scales@d767915)     
#;-)  sf         * 0.5-5      2017-10-31 CRAN (R 3.4.2)                     
#;-)  sp           1.2-5      2017-06-29 CRAN (R 3.4.1)                     
#;-)  stats      * 3.4.2      2017-09-28 local                              
#;-)  stringi      1.1.5      2017-04-07 CRAN (R 3.4.1)                     
#;-)  stringr    * 1.2.0      2017-02-18 CRAN (R 3.4.1)                     
#;-)  tibble     * 1.3.4      2017-08-22 CRAN (R 3.4.1)                     
#;-)  tidyr      * 0.7.2      2017-10-16 CRAN (R 3.4.2)                     
#;-)  tidyverse  * 1.2.1      2017-11-14 CRAN (R 3.4.2)                     
#;-)  tools        3.4.2      2017-09-28 local                              
#;-)  udunits2     0.13       2016-11-17 CRAN (R 3.4.1)                     
#;-)  units        0.4-6      2017-08-27 CRAN (R 3.4.1)                     
#;-)  utils      * 3.4.2      2017-09-28 local                              
#;-)  withr        2.1.0.9000 2017-11-30 Github (jimhester/withr@fe81c00)   
#;-)  XML          3.98-1.9   2017-06-19 CRAN (R 3.4.1)                     
#;-)  xml2         1.1.1      2017-01-24 CRAN (R 3.4.1)                     
#;-)  yaml         2.1.14     2016-11-12 CRAN (R 3.4.1)

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement layers 📈
Projects
None yet
Development

No branches or pull requests

4 participants