Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

I know that the title of this question is a duplicate of this Question and this Question but the solutions over there don't work for me and the error message is (slightly) different:

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

(note the missing part about the missing font)

I tried all suggestions that I found (updating / reinstalling all loaded graphic packages, ggplot2, GGally, and scales, reinitialising the Fonts on Mac OSX by starting in safe mode, moving the Fonts from /Fonts/ (Disabled) back into /Fonts...) but none of it resolved the problem.

The error seems to occure when I plot a ggplot graph with

scale_y_continuous(label=scientific_10)

where scientific_10 is defined as

scientific_10 <- function(x) {
parse(text = gsub("e", " %*% 10^", scientific_format()(x)))
}

Therefore the I suspect that the scales library has something to do with it.

The most puzzling is that the error only occurs each so-and-so many times, maybe each 3rd or 5th time i try to plot the same graph...

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.5 (Mavericks)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gridExtra_2.0.0 scales_0.3.0    broom_0.4.0     tidyr_0.3.1             ggplot2_1.0.1   GGally_0.5.0    dplyr_0.4.3    

loaded via a namespace (and not attached):
 [1] Rcpp_0.11.5      magrittr_1.5     MASS_7.3-43      mnormt_1.5-1         munsell_0.4.2    colorspace_1.2-6 lattice_0.20-33  R6_2.0.1        
 [9] stringr_0.6.2    plyr_1.8.1       tools_3.2.2      parallel_3.2.2       grid_3.2.2       gtable_0.1.2     nlme_3.1-121     psych_1.5.8     
[17] DBI_0.3.1        htmltools_0.2.6  lazyeval_0.1.10  yaml_2.1.13      assertthat_0.1   digest_0.6.8     reshape2_1.4.1   rmarkdown_0.8.1 
[25] labeling_0.3     reshape_0.8.5    proto_0.3-10    

traceback()
35: grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, 
    resolveHJust(x$just, x$hjust), resolveVJust(x$just, x$vjust), 
    x$rot, 0)
34: widthDetails.text(x)
33: widthDetails(x)
32: (function (x) 
{
    widthDetails(x)
})(list(label = expression(5 %*% 10^+5, 7.5 %*% 10^+5, 1 %*% 
    10^+6, 1.25 %*% 10^+6, 1.5 %*% 10^+6), x = 1, y =   c(0.0777214770341215, 
0.291044141334423, 0.504366805634725, 0.717689469935027, 0.931012134235329
), just = "centre", hjust = 1, vjust = 0.5, rot = 0, check.overlap = FALSE, 
    name = "axis.text.y.text.8056", gp = list(fontsize = 9.6, 
        col = "black", fontfamily = "", lineheight = 0.9, font = 1L), 
    vp = NULL))
31: grid.Call.graphics(L_setviewport, vp, TRUE)
30: push.vp.viewport(X[[i]], ...)
like image 643
Latrunculia Avatar asked Dec 11 '15 10:12

Latrunculia


People also ask

How do you fix edge not found error in ggplot?

The error in grid.callc textbounds, as.graphicsannot (x$label), x$x, x$y, : polygon edge not found error message occurs when there is a misplaced font called when using a ggplot object. It is fixed either by putting the font in the needed location or redirecting the call to the correct location.

Why does my ggplot call a font that is not there?

This is not a problem with your code, but in an external file that is not where your code expects it to be. This problem occurs when using the ggplot function and it calls a font that is not where it is supposed to be. One thing that can make this situation particularly troublesome is that it is possible that the font has somehow been moved.

What causes the ordered character vector error when graphing?

For me the error was caused by calling a fairly complex graphing function (see below) that read an ordered character vector as well as a matrix whose row names were supposed to each match a value in the ordered character vector.


2 Answers

I solved it by installing the library extrafont, installing a set of specific fonts and forcing ggplot to use only these fonts:

    require(extrafont)
    # need only do this once!
    font_import(pattern="[A/a]rial", prompt=FALSE)
    require(ggplot2)
    # extending the help file example
    df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30))
    ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))
    plotobj <- ggplot(df, aes(gp, y)) +
      geom_point() +
      geom_point(data = ds, aes(y = mean), colour = 'red', size = 3) + 
      theme(text=element_text(size=16, family="Arial"))
    print(plotobj)
like image 143
SarumObjects Avatar answered Oct 04 '22 02:10

SarumObjects


I experienced the same issue when trying to plot ggplot/grid output to the graph window in Rstudio. However, plotting to an external graphing device seems to work fine.

The external device of choice depends on your system, but the script below, paraphrased from this blog, works for most systems:

a = switch(tolower(Sys.info()["sysname"]),
               "darwin"  = "quartz",
               "linux"   = "x11",
               "windows" = "windows")
options("device" = a)
graphics.off()
rm(a)

and to switch back to using the Rstudio plot window:

options("device"="RStudioGD")
graphics.off()

Note that by switching, you lose any existing plots.

like image 30
Joost Keuskamp Avatar answered Oct 04 '22 04:10

Joost Keuskamp