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 · 61 comments
Closed

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

johngoldin opened this issue Aug 26, 2017 · 61 comments
Labels
bug an unexpected problem or unintended behavior layers 📈

Comments

@johngoldin
Copy link

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

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

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 commented Oct 30, 2017

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

@hadley hadley added bug an unexpected problem or unintended behavior layers 📈 labels Oct 30, 2017
@chris-prener
Copy link

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

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

@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

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

@tbrambor
Copy link

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 commented Feb 15, 2018

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

@tbrambor
Copy link

@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

@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

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

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

Any fix?

@batpigandme
Copy link
Contributor

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

@praveenkumarpgiindia
Copy link

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 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 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 commented May 2, 2018

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

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

slagtermaarten commented May 3, 2018 via email

@slagtermaarten
Copy link

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

@clauswilke
Copy link
Member

Possibly related: #2514

@swood-ecology
Copy link

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

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

@abiyug
Copy link

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

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

@abiyug
Copy link

abiyug commented Nov 11, 2019

Version 1.0.136

@SergeyBaikal
Copy link

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 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 commented Jul 13, 2020

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

@mcui123
Copy link

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

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 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!

@edzer
Copy link
Contributor

edzer commented Jul 29, 2021

I saw the polygon edge not found error again, associated with ggplot() calls in CRAN checks on OSX-arm64, search for stars3.Rmd in https://www.r-project.org/nosvn/R.check/r-release-macos-arm64/stars-00check.html, or for sf5.Rmd in https://www.r-project.org/nosvn/R.check/r-release-macos-arm64/sf-00check.html

@bschilder
Copy link

bschilder commented Jan 6, 2022

The polygon edge not found issue seems to be persisting for me on GH Actions during CMD checks.

The vignette where the error occurs is here.
See GHA output here:

Run options(crayon.enabled = TRUE)
Loading required package: sessioninfo
── R CMD build ─────────────────────────────────────────────────────────────────
* checking for file ‘.../DESCRIPTION’ ... OK
* preparing ‘EWCE’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ... ERROR
Error: --- re-building ‘EWCE.Rmd’ using rmarkdown
Quitting from lines 136-140 (EWCE.Rmd) 
Error: Error: processing vignette 'EWCE.Rmd' failed with diagnostics:
polygon edge not found
--- failed re-building ‘EWCE.Rmd’
--- re-building ‘extended.Rmd’ using rmarkdown
Quitting from lines 45-53 (extended.Rmd) 
Error: Error: processing vignette 'extended.Rmd' failed with diagnostics:
R: GeometryDoesNotContainImage `/private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/RtmpmCdAXZ/Rbuildf0522e15b8e2/EWCE/vignettes/extended_files/figure-html/unnamed-chunk-1-1.png' @ warning/attribute.c/GetImageBoundingBox/532
--- failed re-building ‘extended.Rmd’
SUMMARY: processing the following files failed:
  ‘EWCE.Rmd’ ‘extended.Rmd’
Error: Error: Vignette re-building failed.
Execution halted
Error: Error in proc$get_built_file() : Build process failed
Calls: <Anonymous> ... build_package -> with_envvar -> force -> <Anonymous>
Execution halted
Error: Process completed with exit code 1.

Many thanks,
Brian

Session info.

Loading required package: sessioninfo
─ Session info ───────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.1.2 (2021-11-01)
 os       macOS Big Sur 10.16
 system   x86_64, darwin17.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       UTC
 date     2022-01-06
 pandoc   2.7.3 @ /usr/local/bin/pandoc

─ Packages ───────────────────────────────────────────────────────────────────────────────────────
 ! package                * version        date (UTC) lib source
   abind                    1.4-5          2016-07-21 [1] CRAN (R 4.1.0)
   annotate                 1.72.0         2021-10-26 [1] Bioconductor
   AnnotationDbi            1.56.2         2021-11-09 [1] Bioconductor
   AnnotationHub            3.2.0          2021-10-26 [1] Bioconductor
   askpass                  1.1            2019-01-13 [1] CRAN (R 4.1.0)
   assertthat               0.2.1          2019-03-21 [1] CRAN (R 4.1.0)
   babelgene                21.4           2021-04-26 [1] CRAN (R 4.1.0)
   backports                1.4.1          2021-12-13 [1] CRAN (R 4.1.0)
   recipes                  0.1.17         2021-09-27 [1] CRAN (R 4.1.0)
   rematch                  1.0.1          2016-04-21 [1] CRAN (R 4.1.0)
   rematch2                 2.1.2          2020-05-01 [1] CRAN (R 4.1.0)
   remotes                  2.4.2          2021-11-30 [1] CRAN (R 4.1.0)
   repmis                   0.5            2016-02-07 [1] CRAN (R 4.1.0)
   reshape2                 1.4.4          2020-04-09 [1] CRAN (R 4.1.0)
   rlang                    0.4.12         2021-10-18 [1] CRAN (R 4.1.0)
   rmarkdown                2.11           2021-09-14 [1] CRAN (R 4.1.0)
   RNOmni                   1.0.0          2020-10-05 [1] CRAN (R 4.1.0)
   rpart                    4.1-15         2019-04-12 [2] CRAN (R 4.1.2)
   rprojroot                2.0.2          2020-11-15 [1] CRAN (R 4.1.0)
   RSQLite                  2.2.9          2021-12-06 [1] CRAN (R 4.1.0)
   rstatix                  0.7.0          2021-02-13 [1] CRAN (R 4.1.0)
   rstudioapi               0.13           2020-11-12 [1] CRAN (R 4.1.0)
   RUnit                    0.4.32         2018-05-18 [1] CRAN (R 4.1.0)
   rvcheck                  0.2.1          2021-10-22 [1] CRAN (R 4.1.0)
   S4Vectors                0.32.3         2021-11-21 [1] Bioconductor
   sass                     0.4.0          2021-05-12 [1] CRAN (R 4.1.0)
   scales                   1.1.1          2020-05-11 [1] CRAN (R 4.1.0)
   sctransform              0.3.2          2020-12-16 [1] CRAN (R 4.1.0)
   sessioninfo            * 1.2.2          2021-12-06 [1] CRAN (R 4.1.0)
   shiny                    1.7.1          2021-10-02 [1] CRAN (R 4.1.0)
   SingleCellExperiment     1.16.0         2021-10-26 [1] Bioconductor
   snow                     0.4-4          2021-10-27 [1] CRAN (R 4.1.0)
   sourcetools              0.1.7          2018-04-25 [1] CRAN (R 4.1.0)
   sp                       1.4-6          2021-11-14 [1] CRAN (R 4.1.0)
   SparseM                  1.81           2021-02-18 [1] CRAN (R 4.1.0)
   sparseMatrixStats        1.6.0          2021-10-26 [1] Bioconductor
   spatial                  7.3-14         2021-05-03 [2] CRAN (R 4.1.2)
   splines                  4.1.2          2021-11-01 [?] local
   SQUAREM                  2021.1         2021-01-13 [1] CRAN (R 4.1.0)
 P stats                  * 4.1.2          2021-11-01 [2] local
   stats4                   4.1.2          2021-11-01 [?] local
   stringdist               0.9.8          2021-09-09 [1] CRAN (R 4.1.0)
   stringi                  1.7.6          2021-11-29 [1] CRAN (R 4.1.0)
   stringr                  1.4.0          2019-02-10 [1] CRAN (R 4.1.0)
   SummarizedExperiment     1.24.0         2021-10-26 [1] Bioconductor
   survival                 3.2-13         2021-08-24 [2] CRAN (R 4.1.2)
   sys                      3.4            2020-07-23 [1] CRAN (R 4.1.0)
   tcltk                    4.1.2          2021-11-01 [?] local
   testthat                 3.1.1          2021-12-03 [1] CRAN (R 4.1.0)
   tibble                   3.1.6          2021-11-07 [1] CRAN (R 4.1.0)
   tidyr                    1.1.4          2021-09-27 [1] CRAN (R 4.1.0)
   tidyselect               1.1.1          2021-04-30 [1] CRAN (R 4.1.0)
   timeDate                 3043.102       2018-02-21 [1] CRAN (R 4.1.0)
   tinytex                  0.36           2021-12-19 [1] CRAN (R 4.1.0)
 P tools                    4.1.2          2021-11-01 [2] local
   tzdb                     0.2.0          2021-10-27 [1] CRAN (R 4.1.0)
   usethis                  2.1.5          2021-12-09 [1] CRAN (R 4.1.0)
   utf8                     1.2.2          2021-07-24 [1] CRAN (R 4.1.0)
 P utils                  * 4.1.2          2021-11-01 [2] local
   vctrs                    0.3.8          2021-04-29 [1] CRAN (R 4.1.0)
   viridisLite              0.4.0          2021-04-13 [1] CRAN (R 4.1.0)
   vroom                    1.5.7          2021-11-30 [1] CRAN (R 4.1.0)
   waldo                    0.3.1          2021-09-14 [1] CRAN (R 4.1.0)
   whisker                  0.4            2019-08-28 [1] CRAN (R 4.1.0)
   withr                    2.4.3          2021-11-30 [1] CRAN (R 4.1.0)
   xfun                     0.29           2021-12-14 [1] CRAN (R 4.1.0)
   XML                      3.99-0.8       2021-09-17 [1] CRAN (R 4.1.0)
   xopen                    1.0.0          2018-09-17 [1] CRAN (R 4.1.0)
   xtable                   1.8-4          2019-04-21 [1] CRAN (R 4.1.0)
   XVector                  0.34.0         2021-10-26 [1] Bioconductor
   yaml                     2.2.1          2020-02-01 [1] CRAN (R 4.1.0)
   yulab.utils              0.0.4          2021-10-09 [1] CRAN (R 4.1.0)
   zip                      2.2.0          2021-05-31 [1] CRAN (R 4.1.0)
   zlibbioc                 1.40.0         2021-10-26 [1] Bioconductor

 [1] /Users/runner/work/_temp/Library
 [2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

 P ── Loaded and on-disk path mismatch.

@clauswilke
Copy link
Member

Since this discussion keeps growing though it's not a ggplot2 issue, let me try to move this forward and propose a way somebody could dig deeper and figure out what's going on.

The error is triggered by grid, here:
https://github.com/wch/r-source/blob/b9a69ebd17317a7f16963d4a8b915814fe98ff23/src/library/grid/src/grid.c#L1931

The code tries to find the edge on a convex polygon:
https://github.com/wch/r-source/blob/b9a69ebd17317a7f16963d4a8b915814fe98ff23/src/library/grid/src/grid.c#L1810-L1819

I suspect there are some numerical edge cases that cause the algorithm to fail. If we can figure out what those edge cases are we can try to improve the algorithm or try to make sure it isn't fed bad data.

The way to debug this further would be to attach a break point or otherwise log the input data to the function polygonEdge() when the error branch is called. This should reliably produce a reproducible set of input data that cause polygonEdge() to fail, and then we can figure out whether the problem is that polygonEdge() has problems in edge cases or that it is fed bad data, and then go from there.

In any case, all of this discussion and further digging should happen on an issue tracker for grid. Not sure if one exists.

@edzer
Copy link
Contributor

edzer commented Jan 6, 2022

thanks for the context, Claus! I can also not reproduce @bschilder 's problem on my computer. I would be happy to dive into it, if only I had a Mac!

milanmlft added a commit to statOmics/HDDA21 that referenced this issue Jan 24, 2022
The `polygon edge not found` error that keeps popping up seems to be related to `macOS` only.
See tidyverse/ggplot2#2252
@hadley
Copy link
Member

hadley commented Jan 26, 2022

I had a vague recollection that @pmur002 fixed this in R? But it seems like maybe it is cropping up again, so something may have changed to re-trigger it.

EricMarcon added a commit to EricMarcon/SpatDiv that referenced this issue Jan 28, 2022
Code coverage generates an error when knitting the vignette: polygon edge not found.
Only on MacOS, see tidyverse/ggplot2#2252
@slhck
Copy link

slhck commented Feb 18, 2022

I don't want to create noise on this thread, but seeing that there is no other place for this … I have an RStudio session open with which I can more or less reliably trigger that error. It happens when I have the session open for some time, and something (perhaps switching displays or having had the screensaver enabled) triggers that error whenever I try to plot anything. The error only appears after some time, not immediately after the session is started. Re-starting the R session always fixes it.

So even a simple call to:

ggplot(mpg, aes(x = cyl, y = hwy)) + geom_point()

raises the error:

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

So there you have your set of input data, at least. It is not related to the actual data, and it seems to be an issue with RStudio or R itself.

This is Rstudio 2021.09.2 Build 382 under macOS 12.1 (Apple Silicon) with R 4.1.2 from Homebrew.

bodkan added a commit to bodkan/slendr that referenced this issue Feb 18, 2022
@bschilder
Copy link

FYI this is still occurring.

Screenshot 2022-05-28 at 22 31 46

dtburk added a commit to ipums/ipumsr that referenced this issue Jun 2, 2022
sbashevkin added a commit to sbashevkin/deltareportr that referenced this issue Jun 30, 2022
@pmur002
Copy link
Contributor

pmur002 commented Jul 6, 2022

I finally got this to replicate for me (using @clauswilke simple example) and I think I may have fixed it (r-devel r82550).

Would be great if anyone could confirm the fix.

@pmur002
Copy link
Contributor

pmur002 commented Sep 18, 2022

r-devel r82861has some more improvements to the code around this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior layers 📈
Projects
None yet
Development

No branches or pull requests