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_sf(): "polygon edge not found" intermittent error #2252

Open
johngoldin opened this Issue Aug 26, 2017 · 41 comments

Comments

Projects
None yet
@johngoldin

johngoldin commented Aug 26, 2017

I am using geom_sf to do some county maps based on tigris data. I successfully followed some examples, but when I try to do my local county (New Haven, CT), I got the following error while doing geom_sf():

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
polygon edge not found

I spent some time to create a reproducible error and even learned to use the reprex package. But I didn't actually post the issue. Then I opened my lap top this morning and ran the reprex example again. No error. I couldn't think of any reason why it started working, but it worked so I went on about my business. After several hours of work I hit the same error again, under somewhat different circumstances. Given the earlier inconsistency, I tried running the same call multiple times and did not get the error when the same call was repeated.

I think there is a problem, but I have only a nebulous idea of what the problem is. So I will put my non-reproduced reproducible error code below and maybe I won't be the only person who gets an intermittent "polygon edge not found" error. I realize this is not a terribly helpful issue report.

First, here is a copy of my console showing several variations of a geom_sf() call. I was varying the size parameter because I thought that triggered the error in this case. But if you look closely you can see that I make the identical call to geom_sf() and get an error in one instance and no error in another )with the same parameters (size = 0.1).

ggplot() +

  • geom_sf(data = p2) +
  • geom_sf(data = newhaven_necta, fill = NA, color = "red")

ggplot() +

  • geom_sf(data = p2, size = 1) +
  • geom_sf(data = newhaven_necta, fill = NA, color = "red")
    Warning message:
    In getNamespace("grDevices") : reached elapsed time limit

ggplot() +

  • geom_sf(data = p2, size = 0.1) +
  • geom_sf(data = newhaven_necta, fill = NA, color = "red")
    Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
    polygon edge not found

ggplot() +

  • geom_sf(data = p2, size = 0.3) +
  • geom_sf(data = newhaven_necta, fill = NA, color = "red")

ggplot() +

  • geom_sf(data = p2, size = 0.1) +
  • geom_sf(data = newhaven_necta, fill = NA, color = "red")

OK, next is the description what I thought was going to be a reproducible error:

I am using the tracts function of the tigris package to get shapefiles from the census. I thought the key factor in whether geom_sf gave me an error was whether or not the tracts functions specified cb = FALSE.

library(tigris)
#> To enable 
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
#> 
#> Attaching package: 'tigris'
#> The following object is masked from 'package:graphics':
#> 
#>     plot
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag():    dplyr, stats

options(tigris_class = "sf")

# this interior Connecticut county works OK:
hartford_tigris <- tracts("CT", county = "Hartford", cb = TRUE)
#> 
  
ggplot(hartford_tigris) + geom_sf(data = hartford_tigris)

# New Haven works OK if I specifiy cb = FALSE
nh_tigris <- tracts("CT", county = "New Haven", cb = FALSE)
#> 
  
ggplot(nh_tigris) + geom_sf()

# New Haven fails if cb = TRUE.
# Error message I get is:
# Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
#                      polygon edge not found

nh_tigris_cb <- tracts("CT", county = "New Haven", cb = TRUE)
ggplot(nh_tigris_cb) + geom_sf()

# From the successful example when cb = FALSE, one can see that New Haven county shows with
# a boundary far out into Long Sound. I'm guessing this is related to the polygon edge
# that is causing problems. I tried this for some counties in Rhode Island.
# Washington county fails with "polygon edge not found" when cb = TRUE.
# Newport county does not fail.

# One can examine some other coastal counties. Some have this problem and some don't.
ri_washington_cb <- tracts("RI", county = "Washington", cb = TRUE)
#> 
  
ri_newport_cb <- tracts("RI", county = "Newport", cb = TRUE)
ri_newport <- tracts("RI", county = "Newport")

ggplot(ri_newport_cb) + geom_sf()

ggplot(ri_newport) + geom_sf()

ggplot(ri_washington_cb) + geom_sf()

ggplot(ri_washington) + geom_sf()

Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.1 (2017-06-30)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2017-08-26
#> Packages -----------------------------------------------------------------
#>  package    * version    date       source                            
#>  assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)                    
#>  backports    1.1.0      2017-05-22 CRAN (R 3.4.0)                    
#>  base       * 3.4.1      2017-07-07 local                             
#>  bindr        0.1        2016-11-13 CRAN (R 3.4.0)                    
#>  bindrcpp     0.2        2017-06-17 CRAN (R 3.4.0)                    
#>  bitops       1.0-6      2013-08-17 CRAN (R 3.4.0)                    
#>  broom        0.4.2      2017-02-13 CRAN (R 3.4.0)                    
#>  cellranger   1.1.0      2016-07-27 CRAN (R 3.4.0)                    
#>  colorspace   1.3-2      2016-12-14 CRAN (R 3.4.0)                    
#>  compiler     3.4.1      2017-07-07 local                             
#>  curl         2.8.1      2017-07-21 CRAN (R 3.4.1)                    
#>  datasets   * 3.4.1      2017-07-07 local                             
#>  DBI          0.7        2017-06-18 CRAN (R 3.4.0)                    
#>  devtools     1.13.3     2017-08-02 CRAN (R 3.4.1)                    
#>  digest       0.6.12     2017-01-27 CRAN (R 3.4.0)                    
#>  dplyr      * 0.7.2      2017-07-20 CRAN (R 3.4.1)                    
#>  evaluate     0.10.1     2017-06-24 CRAN (R 3.4.1)                    
#>  forcats      0.2.0      2017-01-23 CRAN (R 3.4.0)                    
#>  foreign      0.8-69     2017-06-22 CRAN (R 3.4.1)                    
#>  ggplot2    * 2.2.1.9000 2017-08-24 Github (tidyverse/ggplot2@41f154f)
#>  glue         1.1.1      2017-06-21 CRAN (R 3.4.1)                    
#>  graphics   * 3.4.1      2017-07-07 local                             
#>  grDevices  * 3.4.1      2017-07-07 local                             
#>  grid         3.4.1      2017-07-07 local                             
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.4.0)                    
#>  haven        1.1.0      2017-07-09 CRAN (R 3.4.1)                    
#>  hms          0.3        2016-11-22 CRAN (R 3.4.0)                    
#>  htmltools    0.3.6      2017-04-28 CRAN (R 3.4.0)                    
#>  httr         1.3.1      2017-08-20 cran (@1.3.1)                     
#>  jsonlite     1.5        2017-06-01 CRAN (R 3.4.0)                    
#>  knitr        1.17       2017-08-10 CRAN (R 3.4.1)                    
#>  lattice      0.20-35    2017-03-25 CRAN (R 3.4.1)                    
#>  lazyeval     0.2.0      2016-06-12 CRAN (R 3.4.0)                    
#>  lubridate    1.6.0      2016-09-13 CRAN (R 3.4.0)                    
#>  magrittr     1.5        2014-11-22 CRAN (R 3.4.0)                    
#>  maptools     0.9-2      2017-03-25 CRAN (R 3.4.0)                    
#>  memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                    
#>  methods    * 3.4.1      2017-07-07 local                             
#>  mnormt       1.5-5      2016-10-15 CRAN (R 3.4.0)                    
#>  modelr       0.1.1      2017-07-24 CRAN (R 3.4.1)                    
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.4.0)                    
#>  nlme         3.1-131    2017-02-06 CRAN (R 3.4.1)                    
#>  parallel     3.4.1      2017-07-07 local                             
#>  pkgconfig    2.0.1      2017-03-21 CRAN (R 3.4.0)                    
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.4.0)                    
#>  psych        1.7.5      2017-05-03 CRAN (R 3.4.1)                    
#>  purrr      * 0.2.3      2017-08-02 CRAN (R 3.4.1)                    
#>  R6           2.2.2      2017-06-17 CRAN (R 3.4.0)                    
#>  rappdirs     0.3.1      2016-03-28 CRAN (R 3.4.0)                    
#>  Rcpp         0.12.12    2017-07-15 CRAN (R 3.4.1)                    
#>  RCurl        1.95-4.8   2016-03-01 CRAN (R 3.4.0)                    
#>  readr      * 1.1.1      2017-05-16 CRAN (R 3.4.0)                    
#>  readxl       1.0.0      2017-04-18 CRAN (R 3.4.0)                    
#>  reshape2     1.4.2      2016-10-22 CRAN (R 3.4.0)                    
#>  rgdal        1.2-8      2017-07-01 CRAN (R 3.4.1)                    
#>  rgeos        0.3-23     2017-04-06 CRAN (R 3.4.0)                    
#>  rlang        0.1.2      2017-08-09 CRAN (R 3.4.1)                    
#>  rmarkdown    1.6        2017-06-15 CRAN (R 3.4.0)                    
#>  rprojroot    1.2        2017-01-16 CRAN (R 3.4.0)                    
#>  rvest        0.3.2      2016-06-17 CRAN (R 3.4.0)                    
#>  scales       0.5.0      2017-08-24 Github (hadley/scales@21bf4c0)    
#>  sf         * 0.5-3      2017-07-30 CRAN (R 3.4.1)                    
#>  sp           1.2-5      2017-06-29 CRAN (R 3.4.1)                    
#>  stats      * 3.4.1      2017-07-07 local                             
#>  stringi      1.1.5      2017-04-07 CRAN (R 3.4.0)                    
#>  stringr      1.2.0      2017-02-18 CRAN (R 3.4.0)                    
#>  tibble     * 1.3.3      2017-05-28 CRAN (R 3.4.0)                    
#>  tidyr      * 0.7.0      2017-08-16 CRAN (R 3.4.1)                    
#>  tidyverse  * 1.1.1      2017-01-27 CRAN (R 3.4.0)                    
#>  tigris     * 0.5.9      2017-08-25 Github (walkerke/tigris@4d6a38a)  
#>  tools        3.4.1      2017-07-07 local                             
#>  udunits2     0.13       2016-11-17 CRAN (R 3.4.0)                    
#>  units        0.4-5      2017-06-15 CRAN (R 3.4.0)                    
#>  utils      * 3.4.1      2017-07-07 local                             
#>  uuid         0.1-2      2015-07-28 CRAN (R 3.4.0)                    
#>  withr        2.0.0      2017-08-19 Github (jimhester/withr@0c4a86d)  
#>  XML          3.98-1.9   2017-06-19 CRAN (R 3.4.1)                    
#>  xml2         1.1.1      2017-01-24 CRAN (R 3.4.0)                    
#>  yaml         2.1.14     2016-11-12 CRAN (R 3.4.0)
@johngoldin

This comment has been minimized.

johngoldin commented Aug 28, 2017

I have even less idea what triggers the "polygon edge not found" error. In the project that I am working on with tigris maps I have made the same call to geom_sf multiple times with no problem and then suddenly have it switch to throwing this error and no longer able to run. As far as I can tell the calling parameters are identical.

@johngoldin

This comment has been minimized.

johngoldin commented Aug 29, 2017

Sigh, one more problem with my attempt to create a repeatable example: to run the tigris and tidycensus calls you have to have a Census API key.

@hadley

This comment has been minimized.

Member

hadley commented Oct 30, 2017

This appears to be some grid bug. Unfortunately I haven't been able to reliably reproduce it either.

@chris-prener

This comment has been minimized.

chris-prener commented Feb 10, 2018

hi @hadley and @johngoldin - I came across this error (and subsequently this issue) while getting ready for a lecture on sf and geom_sf() on Monday in my GIS course.

I have been able to reproduce the error with a set of data that I use for teaching. The data are packaged up and available on GitHub:

devtools::install_github("chris-prener/stlData")

Here is a reprex that I have been using to trigger the error:

library(ggplot2)
library(stlData)

ggplot() +
  geom_sf(data = stlBoundary, fill = "#5d5d5d", color = "#5d5d5d") +
  geom_sf(data = stlHistoric, fill = "#d48a72", color = "#d48a72") +
  geom_sf(data = stlHydro, fill = "#72bcd4", color = "#72bcd4")

The error does not happen every time I re-create the plot. This afternoon, I was able to trigger the error every other time or every third time I created the plot:

> library(ggplot2)
> library(stlData)
> ggplot() +
+   geom_sf(data = stlBoundary, fill = "#5d5d5d", color = "#5d5d5d") +
+   geom_sf(data = stlHistoric, fill = "#d48a72", color = "#d48a72") +
+   geom_sf(data = stlHydro, fill = "#72bcd4", color = "#72bcd4")
> ggplot() +
+   geom_sf(data = stlBoundary, fill = "#5d5d5d", color = "#5d5d5d") +
+   geom_sf(data = stlHistoric, fill = "#d48a72", color = "#d48a72") +
+   geom_sf(data = stlHydro, fill = "#72bcd4", color = "#72bcd4")
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found

I hope this is helpful for you @hadley in terms of getting to the bottom of what is happening! Thanks for all of your hard work on this package (and others) - they are a joy to use and to teach with.

@tbrambor

This comment has been minimized.

tbrambor commented Feb 15, 2018

I ran into the same error on this tutorial for making a map in ggplot: https://github.com/grssnbchr/thematic-maps-ggplot2

Since all data and code is on Github, perhaps that is easily replicable. Specifically, the error appears in this code chunk:

p <- ggplot() +
    # municipality polygons
    geom_polygon(data = map_data, aes(fill = avg_age_15, 
                                      x = long, 
                                      y = lat, 
                                      group = group)) +
    # municipality outline
    geom_path(data = map_data, aes(x = long, 
                                   y = lat, 
                                   group = group), 
              color = "white", size = 0.1) +
    coord_equal() +
    # add the previously defined basic theme
    theme_map() +
    labs(x = NULL, 
         y = NULL,         
         title = "Switzerland's regional demographics",
         subtitle = "Average age in Swiss municipalities, 2015", 
         caption = "Geometries: ThemaKart, BFS; Data: BFS, 2016")

The addition of the labs argument triggers the error.

@hadley

This comment has been minimized.

Member

hadley commented Feb 15, 2018

@tbrambor does it happen every time? If so, can you please make a small reprex that I can run by copying and pasting?

@tbrambor

This comment has been minimized.

tbrambor commented Feb 15, 2018

@hadley . The following reprex shows the error. Oddly, it appears to be related to using a font that is not installed on my system in defining the theme of the plot. If the title and labels of the plot axes are surpressed, the error disappears.

library(ggplot2)
library(mapdata)
#> Loading required package: maps
usa <- map_data("usa")

# Define a theme
theme_map <- function(...) {
  theme_minimal() + theme(text = element_text(family = "Sans"))
}

# Map
ggplot() + geom_polygon(data = usa, aes(x = long, y = lat)) + theme_map()
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "Sans"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "Sans"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "Sans"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "Sans"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "Sans"
#> Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found
@chris-prener

This comment has been minimized.

chris-prener commented Feb 15, 2018

@tbrambor does this error also occur when you do not use the theme_map() you've defined?

@tbrambor

This comment has been minimized.

tbrambor commented Feb 15, 2018

No. If the (incorrect) theme mapping is excluded, the plot works fine. It appears the plotting function tries to use the non-installed font which results in the error.

@hadley

This comment has been minimized.

Member

hadley commented Feb 15, 2018

@tbrambor I suspect that's a different problem which generates the same error

@tbrambor

This comment has been minimized.

tbrambor commented Feb 15, 2018

@hadley. Quite possible. I was just looking for a solution and came across this filed issue here. Feel free to exclude -- I will just adjust the theme to rely on installed fonts and the error is resolved for me. Thank you.

@chris-prener

This comment has been minimized.

chris-prener commented Feb 18, 2018

@hadley I have a little more info on how to replicate this error consistently. I am able to trigger the Error in grid.Call every time when using colorblindr in the console or in a script:

> library(colorblindr)
> library(ggplot2)
> library(stlData)
> map <- ggplot() +
+         geom_sf(data = stlBoundary, fill = "#5d5d5d", color = "#5d5d5d") +
+         geom_sf(data = stlHistoric, fill = "#d48a72", color = "#d48a72") +
+         geom_sf(data = stlHydro, fill = "#72bcd4", color = "#72bcd4")
> cvd_grid(map)
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found

What is a little nuts is that it works fine in code chunks in .Rmd files and with reprex (see below). Perhaps this indicates where the issue is?

library(colorblindr)
#> Loading required package: colorspace
#> Loading required package: ggplot2
library(ggplot2)
library(stlData)

map <- ggplot() +
        geom_sf(data = stlBoundary, fill = "#5d5d5d", color = "#5d5d5d") +
        geom_sf(data = stlHistoric, fill = "#d48a72", color = "#d48a72") +
        geom_sf(data = stlHydro, fill = "#72bcd4", color = "#72bcd4")

cvd_grid(map)

Created on 2018-02-18 by the reprex package (v0.2.0).

@chris-prener

This comment has been minimized.

chris-prener commented Feb 18, 2018

Due to some updates I'm working on to the package I've been using for these reprexes, anyone who wants to investigate this error should install a specific release:

devtools::install_github("chris-prener/stlData", ref = "f51fe10")
@MathieuMarauri

This comment has been minimized.

MathieuMarauri commented Mar 1, 2018

Hello,

I have the same error message:
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found

I am using the following code:

library('sf')
library('ggplot2')
download.file(url = 'biogeo.ucdavis.edu/data/gadm2.8/rds/FRA_adm0.rds', destfile = 'france.rds')
france <- readRDS('france.rds')
france <- st_as_sf(france)
france <- st_simplify(france, preserveTopology = TRUE, dTolerance = 0.005) # just to make things faster
plot_sf <- function() plot(ggplot(data = france) + geom_sf())
for(i in 1:20) plot_sf()

After some runs the error appears. I am not able to reproduce the error each time I run the code but if I run the same code inside an Rmd file then everythink works fine and I get my plots without any error.

I hope this can help.

Cheers,

Mathieu

@stanleynguyen

This comment has been minimized.

stanleynguyen commented Mar 15, 2018

Any fix?

@batpigandme

This comment has been minimized.

Member

batpigandme commented Mar 15, 2018

Not yet. When there is a fix, the issue will be automatically closed. 👍

@praveenkumarpgiindia

This comment has been minimized.

praveenkumarpgiindia commented Apr 6, 2018

I am getting this error in ubuntu 16.04.

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
X11 font -adobe-helvetica-%s-%s---%d-------*, face 1 at size 11 could not be loaded

Is there a solution?

@ben519

This comment has been minimized.

ben519 commented Apr 19, 2018

I'm also fighting with this error and would love to see it fixed. Here's my reproducible example

library(ggplot2)
library(sf)

foo <- structure(
  list(
    GEOID = c("06059001304", "06059001507"), 
    NAME = c("Census Tract 13.04, Orange County, California", "Census Tract 15.07, Orange County, California"), 
    variable = c("B19013_001", "B19013_001"), 
    estimate = c(62681, 74718), 
    moe = c(6958, 6773), 
    geometry = structure(list(structure(list(list(structure(c(
      -117.959178, -117.950463, -117.946112, -117.941757, -117.939393, -117.937861, -117.937573, -117.937399, -117.936428, 
      -117.94373, -117.943661, -117.941746, -117.941747, -117.946103, -117.946106, -117.950457, -117.959174, -117.959178, 
      33.928202, 33.928211, 33.928217, 33.928224, 33.928176, 33.92733, 33.926877, 33.921087, 33.917342, 33.917339, 
      33.918969, 33.920924, 33.923143, 33.923139, 33.924592, 33.924589, 33.92458, 33.928202
    ), 
    .Dim = c(18L, 2L)))), 
    class = c("XY", "MULTIPOLYGON", "sfg")), 
    structure(list(list(structure(c(
      -117.90654, -117.90215,  -117.895607, -117.89396, -117.893943, -117.890304, -117.883038, -117.882687, -117.88002, 
      -117.880136, -117.88291, -117.892765, -117.89766, -117.89586, -117.899903, -117.90672, -117.90654, 33.93183, 33.93194, 
      33.932067, 33.93204, 33.932108, 33.928166, 33.926332, 33.925287, 33.925381, 33.920673, 33.921727, 33.921496, 33.92137, 
      33.92482, 33.924705, 33.92458, 33.93183), 
      .Dim = c(17L, 2L)))), 
      class = c("XY", "MULTIPOLYGON", "sfg"))), 
    class = c("sfc_MULTIPOLYGON", "sfc"), 
    precision = 0, 
    bbox = structure(
      c(-117.959178, 33.917339, -117.88002, 33.932108), 
      .Names = c("xmin", "ymin", "xmax", "ymax"), class = "bbox"), 
    crs = structure(list(
      epsg = 4269L, 
      proj4string = "+proj=longlat +datum=NAD83 +no_defs"), 
      .Names = c("epsg", "proj4string"), 
      class = "crs"
    ), n_empty = 0L)), 
  .Names = c("GEOID", "NAME", "variable", "estimate", "moe", "geometry"), 
  row.names = 1:2, 
  class = c("sf", "data.frame"), 
  sf_column = "geometry", 
  agr = structure(
    c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_), 
    .Names = c("GEOID", "NAME", "variable", "estimate", "moe"), 
    .Label = c("constant", "aggregate", "identity"), class = "factor")
)

foo

# If you run the code below about 10 times, it should error at least once and work properly at least once
ggplot(data = foo) + geom_sf()
@hadley

This comment has been minimized.

Member

hadley commented Apr 26, 2018

This appears to be a low-level grid problem. I don't think there's much hope of getting it fixed unless someone can generate a minimal reprex using only grid calls.

@hadley

This comment has been minimized.

Member

hadley commented May 2, 2018

Quick question - has anyone seen this problem outside of RStudio?

@karawoo

This comment has been minimized.

Member

karawoo commented May 2, 2018

I just tried @chris-prener's reprex in both RStudio and ESS and got the error in RStudio but not ESS.

@slagtermaarten

This comment has been minimized.

slagtermaarten commented May 3, 2018

@slagtermaarten

This comment has been minimized.

slagtermaarten commented May 3, 2018

It's hard for me to provide a reprex because some external factor/program seems to induce the onset of "Polygon edge not found" errors, after which the only remedy I've found is to reboot my machine. Restarting R won't help. I'm on a 2015 MacBook Pro running High Sierra.

@praveenkumarpgiindia

This comment was marked as outdated.

praveenkumarpgiindia commented May 3, 2018

@clauswilke

This comment has been minimized.

Member

clauswilke commented May 12, 2018

Possibly related: #2514

@swood-ecology

This comment has been minimized.

swood-ecology commented May 24, 2018

I'm not sure if this solves anyone's problems, but I've noticed that if I ever have a chr variable with spaces in the string then it will generate this error--even if I'm not calling that variable at all in what I'm doing. As long as it's present in the data object I get the error, when I drop those variables it goes away.

@clauswilke

This comment has been minimized.

Member

clauswilke commented May 24, 2018

I seem to have stumbled across a minimal ggplot2 example to generate this problem. The following code causes this error for me every time:

library(ggplot2)
ggplot(data.frame(), aes(1, 1)) + theme(text = element_text(family = 0))
#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "0"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "0"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "0"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "0"

#> Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
#> no font could be found for family "0"
#> Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found

Traceback:

> traceback()
32: grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, 
        resolveHJust(x$just, x$hjust), resolveVJust(x$just, x$vjust), 
        x$rot, 0)
31: widthDetails.text(x)
30: widthDetails(x)
29: (function (x) 
    {
        widthDetails(x)
    })(list(label = c("0.50", "0.75", "1.00", "1.25", "1.50"), x = c(1, 
    1, 1, 1, 1), y = c(0, 0.25, 0.5, 0.75, 1), just = "centre", hjust = 1, 
        vjust = 0.5, rot = 0, check.overlap = FALSE, name = "GRID.text.863", 
        gp = list(fontsize = 8.8, col = "grey30", fontfamily = "0", 
            lineheight = 0.9, font = c(plain = 1L)), vp = NULL))
28: grid.Call.graphics(C_setviewport, vp, TRUE)
27: push.vp.viewport(vp$parent, recording)
26: push.vp(vp$parent, recording)
25: push.vp.vpTree(X[[i]], ...)
24: FUN(X[[i]], ...)
23: lapply(vps, push.vp, recording)
22: pushViewport(vp, recording = FALSE)
21: pushgrobvp.viewport(x$vp)
20: pushgrobvp(x$vp)
19: pushvpgp(x)
18: preDraw.gTree(list(widths = list(0, list(fname = "+", arg1 = 1, 
        arg2 = list(fname = "*", arg1 = 0, arg2 = 0.0350748697916667)), 
        2.2), heights = 1, name = "axis.text.y.left..titleGrob.865", 
        gp = NULL, vp = list(parent = list(x = 0.5, y = 0.5, width = 1, 
            height = 1, justification = "centre", gp = list(fontsize = 8.8, 
                col = "grey30", fontfamily = "0", lineheight = 0.9, 
                font = c(plain = 1L)), clip = FALSE, xscale = c(0, 
            1), yscale = c(0, 1), angle = 0, layout = list(nrow = 1L, 
                ncol = 3L, widths = list(0, list(fname = "+", arg1 = 1, 
                    arg2 = list(fname = "*", arg1 = 0, arg2 = 0.0350748697916667)), 
                    2.2), heights = 1, respect = FALSE, valid.respect = 0L, 
                respect.mat = c(0L, 0L, 0L), just = "centre", valid.just = c(0.5, 
                0.5)), layout.pos.row = NULL, layout.pos.col = NULL, 
            valid.just = c(0.5, 0.5), valid.pos.row = NULL, valid.pos.col = NULL, 
            name = "GRID.VP.118"), children = list(list(x = 0.5, 
            y = 0.5, width = 1, height = 1, justification = "centre", 
            gp = list(), clip = FALSE, xscale = c(0, 1), yscale = c(0, 
            1), angle = 0, layout = NULL, layout.pos.row = NULL, 
            layout.pos.col = c(2L, 2L), valid.just = c(0.5, 0.5), 
            valid.pos.row = NULL, valid.pos.col = c(2L, 2L), name = "GRID.VP.119"))), 
        children = list(GRID.text.863 = list(label = c("0.50", "0.75", 
        "1.00", "1.25", "1.50"), x = c(1, 1, 1, 1, 1), y = c(0, 0.25, 
        0.5, 0.75, 1), just = "centre", hjust = 1, vjust = 0.5, rot = 0, 
            check.overlap = FALSE, name = "GRID.text.863", gp = list(
                fontsize = 8.8, col = "grey30", fontfamily = "0", 
                lineheight = 0.9, font = c(plain = 1L)), vp = NULL)), 
        childrenOrder = "GRID.text.863"))
17: (function (x) 
    {
        UseMethod("preDraw")
    })(list(widths = list(0, list(fname = "+", arg1 = 1, arg2 = list(
        fname = "*", arg1 = 0, arg2 = 0.0350748697916667)), 2.2), 
        heights = 1, name = "axis.text.y.left..titleGrob.865", gp = NULL, 
        vp = list(parent = list(x = 0.5, y = 0.5, width = 1, height = 1, 
            justification = "centre", gp = list(fontsize = 8.8, col = "grey30", 
                fontfamily = "0", lineheight = 0.9, font = c(plain = 1L)), 
            clip = FALSE, xscale = c(0, 1), yscale = c(0, 1), angle = 0, 
            layout = list(nrow = 1L, ncol = 3L, widths = list(0, 
                list(fname = "+", arg1 = 1, arg2 = list(fname = "*", 
                    arg1 = 0, arg2 = 0.0350748697916667)), 2.2), 
                heights = 1, respect = FALSE, valid.respect = 0L, 
                respect.mat = c(0L, 0L, 0L), just = "centre", valid.just = c(0.5, 
                0.5)), layout.pos.row = NULL, layout.pos.col = NULL, 
            valid.just = c(0.5, 0.5), valid.pos.row = NULL, valid.pos.col = NULL, 
            name = "GRID.VP.118"), children = list(list(x = 0.5, 
            y = 0.5, width = 1, height = 1, justification = "centre", 
            gp = list(), clip = FALSE, xscale = c(0, 1), yscale = c(0, 
            1), angle = 0, layout = NULL, layout.pos.row = NULL, 
            layout.pos.col = c(2L, 2L), valid.just = c(0.5, 0.5), 
            valid.pos.row = NULL, valid.pos.col = c(2L, 2L), name = "GRID.VP.119"))), 
        children = list(GRID.text.863 = list(label = c("0.50", "0.75", 
        "1.00", "1.25", "1.50"), x = c(1, 1, 1, 1, 1), y = c(0, 0.25, 
        0.5, 0.75, 1), just = "centre", hjust = 1, vjust = 0.5, rot = 0, 
            check.overlap = FALSE, name = "GRID.text.863", gp = list(
                fontsize = 8.8, col = "grey30", fontfamily = "0", 
                lineheight = 0.9, font = c(plain = 1L)), vp = NULL)), 
        childrenOrder = "GRID.text.863"))
16: grid.Call.graphics(C_setviewport, vp, TRUE)
15: push.vp.viewport(X[[i]], ...)
14: FUN(X[[i]], ...)
13: lapply(vps, push.vp, recording)
12: pushViewport(vp, recording = FALSE)
11: pushgrobvp.viewport(x$vp)
10: pushgrobvp(x$vp)
9: pushvpgp(x)
8: preDraw.gTree(x)
7: preDraw(x)
6: drawGTree(x)
5: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
4: grid.draw.gTree(gtable)
3: grid.draw(gtable) at plot.r#170
2: print.ggplot(x)
1: function (x, ...) 
   UseMethod("print")(x)

The error arises in widthDetails(), but curiously I can call widthDetails() on a text grob with fontfamily = 0 without this error:

library(grid)
x <- textGrob("abcd", gp = gpar(fontfamily = 0))
widthDetails(x)
#> [1] 0.361409505208333inches
@dpseidel

This comment has been minimized.

Member

dpseidel commented Jun 15, 2018

@clauswilke I wonder if the inability to reproduce the "polygon edge not found" with widthDetails and textGrob is related to the fact the grid.text calls C_text not C_textBounds(see warnings upon drawing with grid.text below). Is there a way to build a minimum grob that calls C_textBounds?

library(grid)
g <- grid.text("abcd", gp = gpar(fontfamily = 0), draw = FALSE) # equivalent to textGrob
widthDetails(g)
#> [1] 0.361409505208333inches

grid.text("abcd", gp = gpar(fontfamily = 0)) # warnings but no error upon draw
#> Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x,
#> x$y, : no font could be found for family "0"

#> Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x,
#> x$y, : no font could be found for family "0"

#> Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x,
#> x$y, : no font could be found for family "0"

Created on 2018-06-15 by the reprex package (v0.2.0).

I'm super interested in understanding this "polygon edge" error, it seems to crop up a lot for various (font related) reasons - indeed I just found it on display in current ggplot2 documentation.

@clauswilke

This comment has been minimized.

Member

clauswilke commented Jun 15, 2018

I've never dug this deep into grid code and I don't know. Maybe @pmur002 has an idea?

@pmur002

This comment has been minimized.

Contributor

pmur002 commented Jun 17, 2018

Just some info: I think this is a Quartz-specific bug (that's where the "no font could be found for family" warning comes from), which is not helping me to replicate it (not having a mac). If anyone could produce this on a non-Quartz device (ideally a linux-Cairo device) that would be helpful.

@hadley

This comment has been minimized.

Member

hadley commented Jun 18, 2018

I now think this is only incidentally related to sf data - I think the source of the problem is the labels which use the degree symbol. That should make it easier to generate a simple reprex, which @dpseidel is working on.

@clauswilke

This comment has been minimized.

Member

clauswilke commented Jun 19, 2018

@pmur002 I think you're right. I only see the error when using the quartz device. It would be good to know if anybody has seen the error on a different device.

library(ggplot2)
pdf(NULL) # warnings about invalid font family but no error
ggplot(data.frame(), aes(1, 1)) + theme(text = element_text(family = 0))
#> There were 50 or more warnings (use warnings() to see the first 50)
dev.off()
#> null device 
#>           1 

png() # uses quartz on OS X, "polygon edge not found" error
ggplot(data.frame(), aes(1, 1)) + theme(text = element_text(family = 0))
#> Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
#>   polygon edge not found
#> In addition: Warning messages:
#> 1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#>   no font could be found for family "0"
#> 2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#>   no font could be found for family "0"
#> 3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#>   no font could be found for family "0"
#> 4: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#>   no font could be found for family "0"
#> 5: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
#>   no font could be found for family "0"
dev.off()
#> null device 
#>           1 

Cairo::CairoPNG() # no warnings or error
ggplot(data.frame(), aes(1, 1)) + theme(text = element_text(family = 0))
dev.off()
#> null device 
#>           1 
@dpseidel

This comment has been minimized.

Member

dpseidel commented Jun 20, 2018

So, I'm still digging this one out - I don't yet have a true reprex but I've gathered additional data on this issue that may be helpful:

  • I can confirm that the polygon edge error (separate from font family warnings) can be caused by axis tick labels alone and thus, as suspected, is likely only incidental to geom_sf which by default sets the labels to include the degree symbol using the degree_label column from st_graticule(). When I replicated a simple, label-only, ggplot call , I got roughly a 20% failure rate. See my code below in Testing Examples dropdown.
  • Tested with a basic geom_sf plot, I got similar error rates. I can prevent all failures by simply changing or removing the axis tick labels.
  • Interestingly, it seems I can cause marginally higher rates of failure (~35%) with simple superscripts in axis tick labels, in the style of scales::math_format, which makes me mildly surprised I haven’t seen this problem more frequently from scales users (a quick review of SO shows 2 recent issues related to scales and this error). Also interesting, the degree symbol alone will not cause the error.
  • As expected, font family is unrelated in this case. Explicitly setting the font family will not prevent the error. I tested “Arial”, “Times”, and “Helvetica” (default).
  • The error occurs, unsurprisingly, whether or not testing labels on the x or y axis, however the traceback will indicate heightDetails rather than widthDetails if the example is limited to the x axis, as they are below.
  • It does indeed seem to be a quartz issue, and potentially only when combined with the RstudioGD. Using the same label-only ggplot and basic geom_sf calls, I cannot seem to replicate it outside of the RstudioGD on my MacOSX. I got no errors when trying the following:
    • Plotting in the R GUI or from terminal on my Mac
    • Plotting using the png() device from Rstudio and terminal with type= quartz, cairo, and Xlib individually (also on my Mac).
    • Plotting using RstudioGD and my windows machine
    • Plotting using Rstudio Cloud (Ubuntu 16.04.4 LTS; RstudioGD and cairo)
Testing Examples
library(ggplot2) # version 2.2.1.9000
# system info
getOption("bitmapType")
#> [1] "quartz"
getOption("device")
#> [1] "RStudioGD"
 
#  testing function
plot_ok <- function(p) {
  tryCatch({
    print(p)
    TRUE
  }, error = function(e) FALSE)
}

# blank plot
blank_base <- ggplot(data.frame(), aes(1, 1)) +
  theme(
    axis.title.y = element_blank(),
    axis.title.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank(),
    panel.background = element_blank(),
    panel.grid = element_blank()
  )

# with "graticule" labels matching output of sf::st_graticule
# I've seen a range of 14-20% failure
p <- blank_base + scale_x_continuous(labels = c(
  expr(160 * degree * W), expr(140 * degree * W),
  expr(120 * degree * W), expr(100 * degree * W), 
  expr(0 * degree * W)
))
sim <- replicate(50, plot_ok(p))
sum(sim) / 50
#> [1] 0.8
 
# with superscripts -- ala scales::math_format
# I've seen failure ranging from 30-45%
q <- blank_base + scale_x_continuous(labels = c(
  expr(10^`1`), expr(10^`2`),
  expr(10^`3`), expr(10^`4`),
  expr(10^`5`)
))
sim <- replicate(50, plot_ok(q))
sum(sim) / 50
#> [1] 0.56
 
# interestingly, the degree symbol alone does not cause the error
r <- blank_base + scale_x_continuous(labels = c(
	expr(degree), expr(degree),
  	expr(degree), expr(degree),
  expr(degree)
))
sim <- replicate(50, plot_ok(r))
sum(sim) / 50
#> [1] 1
Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.5.0 (2018-04-23)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/Los_Angeles         
#>  date     2018-06-19
#> Packages -----------------------------------------------------------------
#>  package   * version date       source        
#>  backports   1.1.2   2017-12-13 CRAN (R 3.5.0)
#>  base      * 3.5.0   2018-04-24 local         
#>  compiler    3.5.0   2018-04-24 local         
#>  datasets  * 3.5.0   2018-04-24 local         
#>  devtools    1.13.5  2018-02-18 CRAN (R 3.5.0)
#>  digest      0.6.15  2018-01-28 CRAN (R 3.5.0)
#>  evaluate    0.10.1  2017-06-24 CRAN (R 3.5.0)
#>  graphics  * 3.5.0   2018-04-24 local         
#>  grDevices * 3.5.0   2018-04-24 local         
#>  htmltools   0.3.6   2017-04-28 CRAN (R 3.5.0)
#>  knitr       1.20    2018-02-20 CRAN (R 3.5.0)
#>  magrittr    1.5     2014-11-22 CRAN (R 3.5.0)
#>  memoise     1.1.0   2017-04-21 CRAN (R 3.5.0)
#>  methods   * 3.5.0   2018-04-24 local         
#>  Rcpp        0.12.17 2018-05-18 CRAN (R 3.5.0)
#>  rmarkdown   1.10    2018-06-11 cran (@1.10)  
#>  rprojroot   1.3-2   2018-01-03 CRAN (R 3.5.0)
#>  stats     * 3.5.0   2018-04-24 local         
#>  stringi     1.2.2   2018-05-02 CRAN (R 3.5.0)
#>  stringr     1.3.1   2018-05-10 CRAN (R 3.5.0)
#>  tools       3.5.0   2018-04-24 local         
#>  utils     * 3.5.0   2018-04-24 local         
#>  withr       2.1.2   2018-03-15 CRAN (R 3.5.0)
#>  yaml        2.1.19  2018-05-01 CRAN (R 3.5.0)
@chris-prener

This comment has been minimized.

chris-prener commented Jun 20, 2018

Just wanted to thank @dpseidel @clauswilke and @hadley for continuing to try and sort this out - I wish I could be more help!

@dpseidel

This comment has been minimized.

Member

dpseidel commented Aug 10, 2018

Alright @tmastny and I have done a fair bit of digging. Here's what we know so far:

This bug is triggered when plotting expressions as your axis tick labels and only in combination with the quartz bitmapType and the RstudioGD. The error gets returned by an internal grid function, polygonEdge, when it is fed NaNs produced earlier in a function gridText by function textRect. The NaNs are produced when GEExpressionHeight(expr, gc, dd) (occasionally, seemingly randomly) returns Inf or NaN. These functions are taking the expression (the text itself), the graphics context, and the device. @hadley suspects the intermittent failure is likely cause by some memory fault internal to the RstudioGD - possibly caused by the metricInfo function. This will likely need to be turned over to the IDE team for further debugging,

The code for a minimal ggplot2 reprex I've used to debug this is posted as a gist.

@rundel

This comment has been minimized.

rundel commented Sep 2, 2018

I was just messing around with this a little and can confirm that I am seeing the issue crop up from both GEExpressionHeight and GEExpressionWidth sometimes returning Inf and nan using the reprex above and a slightly modified version of grid.

Running this way it also appears that the issue is random (at this level) since adding additional identical calls to GEExpressionHeight and GEExpressionWidth within the same scope can produce multiple successes before a failure.

This suggests a somewhat hacky solution to this issue would be to update grid such that it includes a loop around these calculations that will reattempt them in the case of a non-finite return value with a small limit on the maximum possible attempts. Something along the lines of the following worked for me (modified in textRect in grid's util.c):

if (isExpression(text)) {
    SEXP expr = VECTOR_ELT(text, i % LENGTH(text));
    
    for(int j=0; j < 5; j=j+1) {
      w = fromDeviceWidth(GEExpressionWidth(expr, gc, dd),
                          GE_INCHES, dd);
      h = fromDeviceHeight(GEExpressionHeight(expr, gc, dd),
                           GE_INCHES, dd);
      
      if (isfinite(w) && isfinite(h))
        break;
    }
    
    if (!isfinite(w) || !isfinite(h))
      error(_("non-finite value for text width or height"));
 }

This only alleviates this specific symptom/error and doesn't address the actual underlying problem but at least it let me run the plot in the reprex without seeing the error.

However, to muddy the waters a bit further if I repeatedly run the reprex with this fix in place I eventually see the following error:

 Error in grid.Call.graphics(C_setviewport, vp, TRUE) : 
  non-finite location and/or size for viewport 
@rundel

This comment has been minimized.

rundel commented Sep 3, 2018

@kevinushey awesome, I got as far as GEMetricInfo before getting lost in the RStudio graphics internals

@bakaburg1

This comment has been minimized.

bakaburg1 commented Oct 3, 2018

Hello, maybe my case could be helpful for you to troubleshoot the problem. I have a complex plot with this structure:

grid.arrange(
    plot.1 + labs(x = 'xlab', y = 'ylab', title = 'title),
    arrangeGrob(plot.2, plot.3, plot.4, plot.5),
    nrow = 1,
    widths = c(1,2)
)

which trigger the error polygon edge not found with no further explanations. I run some experiments:

  • Using arrangeGrob and then plotting the resulted plot with plot or grid arrange -> error.
  • if I encapsulate the call in a tryCatch -> ok.
  • if I remove the title option from labs() -> ok.
  • if I remove title from labs() ang move it to a ggtitle() -> ok.
  • if I remove the arrangeGrob(plot.2, plot.3, plot.4, plot.5) line it -> ok.
  • also plot.1 + labs(x = 'xlab', y = 'ylab', title = 'title) by it self -> ok

So it seems ggplot doesn't like that the title options it's used in labs()

OR AT LEAST I THOUGHT SO!
after many consistent experiments it started to work again suddenly with the initial code!!

I am on OsX Mojave

@jebyrnes

This comment has been minimized.

jebyrnes commented Oct 9, 2018

I am on OsX Mojave

Yeah, I had this problem licked until I upgraded to Mojave. There's something problematic there.

@bakaburg1

This comment has been minimized.

bakaburg1 commented Oct 11, 2018

My 2 cents. After updating to RStudio 1.2.1031 I'm not experiencing the problem anymore (for me it was triggered by other operations, again intermittently, not geom_sf). But given the seemingly random nature of the problem, it's hard to say whether was the update to solve it.

@yutannihilation

This comment has been minimized.

Member

yutannihilation commented Oct 11, 2018

it's hard to say whether was the update to solve it.

IIUC, updating RStudio does matter since the Preview version contains this workaround. :)

rstudio/rstudio#3419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment