R grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, 中的错误:绘图时找不到多边形边缘

Chi*_*chi 3 fonts r ggplot2

我知道还有其他关于此的帖子,但我已经尝试了他们建议的所有内容,但没有任何效果,我以前没有这个问题,现在突然之间,在运行我之前运行的代码时,我开始遇到这个问题。我认为这与我正在创建的主题有关,但我不明白为什么这之前工作得很好,现在却给了我这个问题。另外,有时会出现问题,有时不会……再次使用相同的代码。超级奇怪!

theme_Publication <- function(base_size = 15, base_family = 'sans') {
  (theme_foundation(base_size = base_size, base_family = base_family)
   + theme(plot.title = element_text(size = rel(1.2), face = 'bold', hjust = 0.5),
           text = element_text(),
           panel.background = element_rect(colour = NA),
           plot.background = element_rect(colour = NA),
           panel.border = element_rect(colour = NA),
           axis.title = element_text(face = 'bold', size = rel(1)),
           axis.title.y = element_text(angle=90,vjust =2),
           axis.title.x = element_text(vjust = -0.2),
           axis.text = element_text(size = rel(0.8)),
           axis.line = element_line(colour='black', size = 1.2),
           axis.ticks = element_line(),
           panel.grid.major = element_blank(),
           panel.grid.minor = element_blank(),
           legend.key = element_rect(colour = NA),
           legend.position = 'right',
           legend.key.size= unit(0.2, 'cm'),
           legend.margin = margin(t = 0, unit='cm'),
           legend.title = element_text(face='italic'),
           plot.margin=unit(c(2.5,5,2.5,5),'mm'),
           strip.background=element_rect(colour='#F0F0F0',fill='#F0F0F0'),
           strip.text = element_text(face='bold')
   )
  )
}

df <- data.frame(x = c(71.33,   74.98 ,  80 , 85.35  , 90.03),
                 y = c(119.17,  107.73 , 99.72 ,  75,  54.59))

(b = ggplot(df)+
    geom_col(aes(x, y), position = "dodge") +
    xlab('x') +
    ylab('y') +
    labs(title = 'Example') +
    theme_Publication())

Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,有时 R 会给出以下消息:

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

我找到了解决方法:

(b = ggplot(df)+
geom_col(aes(x, y), position = "dodge") +
xlab('x') +
ylab('y') +
labs(title = 'Example') +
theme_Publication() + 
theme(text = element_text(family = "Arial")))
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做,则无法将此图像另存为 pdf,这是一个问题。我尝试下载“extrafont”包,但是当我查找该包的字体时,列表为空。请问有人可以帮助我吗?这让我发疯。

谢谢你!

Qui*_*ten 7

首先安装软件包以获取 Arial 等字体:

library(ggthemes)
library(extrafont)
library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
Run Code Online (Sandbox Code Playgroud)

之后加载字体:

# Load fonts
loadfonts(quiet = T)
fonts()
Run Code Online (Sandbox Code Playgroud)

由于字体很多,输出很小:

fonts()
  [1] ".SF Compact Rounded"     ".Keyboard"               ".New York"               ".SF Arabic"             
  [5] ".SF Compact"             "System Font"             ".SF NS Mono"             ".SF NS Rounded"         
  [9] "Academy Engraved LET"    "Andale Mono"             "Apple Braille"           "AppleMyungjo"           
 [13] "Arial Black"             "Arial"                   "Arial Narrow"            "Arial Rounded MT Bold"  
 [17] "Arial Unicode MS"        "Bodoni Ornaments"        "Bodoni 72 Smallcaps"     ""                      
Run Code Online (Sandbox Code Playgroud)

然后运行这段代码:

theme_Publication <- function(base_size = 15, base_family = 'sans') {
  (theme_foundation(base_size = base_size, base_family = base_family)
   + theme(plot.title = element_text(size = rel(1.2), face = 'bold', hjust = 0.5),
           text = element_text(),
           panel.background = element_rect(colour = NA),
           plot.background = element_rect(colour = NA),
           panel.border = element_rect(colour = NA),
           axis.title = element_text(face = 'bold', size = rel(1)),
           axis.title.y = element_text(angle=90, vjust = 0.5, size = 10),
           axis.title.x = element_text(vjust = -0.5, size = 10),
           axis.text = element_text(size = rel(0.8)),
           axis.line = element_line(colour='black', size = 1.2),
           axis.ticks = element_line(),
           panel.grid.major = element_blank(),
           panel.grid.minor = element_blank(),
           legend.key = element_rect(colour = NA),
           legend.position = 'right',
           legend.key.size= unit(0.2, 'cm'),
           legend.margin = margin(t = 0, unit='cm'),
           legend.title = element_text(face='italic'),
           plot.margin=unit(c(2.5,5,2.5,5),'mm'),
           strip.background=element_rect(colour='#F0F0F0',fill='#F0F0F0'),
           strip.text = element_text(face='bold')
   )
  )
}

df <- data.frame(x = c(71.33,   74.98 ,  80 , 85.35  , 90.03),
                 y = c(119.17,  107.73 , 99.72 ,  75,  54.59))

(b = ggplot(df)+
    geom_col(aes(x, y), position = "dodge") +
    xlab('x') +
    ylab('y') +
    labs(title = 'Example') +
    theme_Publication() +
    theme(text = element_text(family = "Arial")))

# Set working directory
setwd("/Users/name/Downloads/")
pdf("Your_plot.pdf")
# plot
b
dev.off() 
Run Code Online (Sandbox Code Playgroud)

带有 Arial 文本的输出图:

在此输入图像描述