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

Closed
johngoldin opened this issue Aug 26, 2017 · 52 comments
Closed

geom_sf(): "polygon edge not found" intermittent error #2252

johngoldin opened this issue Aug 26, 2017 · 52 comments

Comments

@johngoldin
Copy link

@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
Copy link
Author

@johngoldin 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
Copy link
Author

@johngoldin 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
Copy link
Member

@hadley 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
Copy link

@chris-prener 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
Copy link

@tbrambor 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
Copy link
Member

@hadley 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
Copy link

@tbrambor 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
Copy link

@chris-prener chris-prener commented Feb 15, 2018

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

@tbrambor
Copy link

@tbrambor 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
Copy link
Member

@hadley hadley commented Feb 15, 2018

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

@tbrambor
Copy link

@tbrambor 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
Copy link

@chris-prener 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
Copy link

@chris-prener 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
Copy link

@MathieuMarauri 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
Copy link

@stanleynguyen stanleynguyen commented Mar 15, 2018

Any fix?

@batpigandme
Copy link
Member

@batpigandme batpigandme commented Mar 15, 2018

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

@praveenkumarpgiindia
Copy link

@praveenkumarpgiindia 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
Copy link

@ben519 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
Copy link
Member

@hadley 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
Copy link
Member

@hadley hadley commented May 2, 2018

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

@karawoo
Copy link
Member

@karawoo 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
Copy link

@slagtermaarten slagtermaarten commented May 3, 2018

@slagtermaarten
Copy link

@slagtermaarten 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 has been hidden.

@clauswilke
Copy link
Member

@clauswilke clauswilke commented May 12, 2018

Possibly related: #2514

@swood-ecology
Copy link

@swood-ecology 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
Copy link
Member

@clauswilke 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
Copy link
Member

@dpseidel 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
Copy link

@rundel 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
Copy link

@rundel rundel commented Sep 3, 2018

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

@bakaburg1
Copy link

@bakaburg1 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
Copy link

@jebyrnes 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
Copy link

@bakaburg1 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
Copy link
Member

@yutannihilation 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

@mobreza
Copy link

@mobreza mobreza commented May 26, 2019

Updating RStudio didn't make a difference, but another side-effect was that axis labels were not rendering.

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

Everything is fine after enabling fonts (Arial, Verdana and Times New Roman) in the Font Book app on Mojave (10.14.5) based on https://discussions.apple.com/thread/8420562

@hadley
Copy link
Member

@hadley hadley commented Jun 18, 2019

RStudio now has a hack that fixes this at least 95% of the time, and we're working on a broader approach to fix the underlying problem (which isn't actually in ggplot2), so I'm going to close this issue.

@abiyug
Copy link

@abiyug abiyug commented Nov 8, 2019

Just came across this issue from RStudio IDE. The error is sporadic, sometimes it works, at other times it throws error. To work around this issue add "theme_void()" before geom_sf.
Example:

ggplot(data = df_sf) +
   **theme_void() +**
   geom_sf()

I am suspecting a "graphic Device issue on RStudio IDE"

@kevinushey
Copy link
Contributor

@kevinushey kevinushey commented Nov 8, 2019

In theory this should be fixed in the latest release of RStudio. What version are you using?

@abiyug
Copy link

@abiyug abiyug commented Nov 11, 2019

Version 1.0.136

@SergeyBaikal
Copy link

@SergeyBaikal SergeyBaikal commented Apr 22, 2020

I have problem:
grid.Call.graphics(C_rect, x$x, x$y, x$width, x$height, resolveHJust(x$just, :

R v. 3.6.1
No translucent drawings when saved (emf format).
Packages UpSetR, ggplot2
How to fix it?

@dcaud
Copy link

@dcaud dcaud commented May 15, 2020

I see this is closed, but I'm still having this problem -- "polygon edge not found" error is intermittent. Reprex is hard, but did any of the reprexes above get tested and resolved?

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

RStudio, Mac....

@cramirezs
Copy link

@cramirezs cramirezs commented Jul 13, 2020

Hi. This problem was solved for me after I removed the values that were infinite.

@mcui123
Copy link

@mcui123 mcui123 commented Aug 28, 2020

@tbrambor I have encountered this exact issue and couldn't figure out why for several days. Your comment was spot on - it helped me solve the issue! Thanks!!

The solution that worked for me: I was using a font that is specific to my company. I replaced that font with a regular system font (e.g. Arial) throughout server.R & UI.R, and the bug was fixed.

No idea why that company font is not available on my company laptop, but that solved my issue. Hope that helps.

@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.

@gstaniak
Copy link

@gstaniak gstaniak commented Sep 15, 2020

For what it's worth, I saw this issue again yesterday. The environment is Mac OS X Catalina. Appeared suddenly, then was reproducible 100% (the only way to see anything plotted was to use theme_void()), and there were the additional warnings about fonts. In retrospect, this happened in the middle of a session after I uninstalled a bunch of unneeded software from the Mac. I googled for the error, tried workarounds, upgraded R and RStudio to latest stable, nothing helped. I got rid of it by rebooting the computer. Looks like it may be system related, but who knows how.

@j450h1
Copy link

@j450h1 j450h1 commented Dec 31, 2020

Had this error. Found the Google Font that missing and installed it on my Mac. Then restarted the R session and the code works!

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

Successfully merging a pull request may close this issue.

None yet