0

我正在尝试使用一种字体,ggplot因为我只能通过extrafont包装。然后当我想使用cowplot包组合多个图时,我总是会出现大量的错误:

46: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x65
47: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x63
48: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x69
49: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x65
50: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x73

请注意,该包确实会产生输出(即并排的绘图),但错误消息与我有关。

到目前为止我尝试了什么:

  • 使用安装字体extrafont::font_install()
  • 使用加载字体extrafont::loadfonts()

我已经安装了extrafontextrafontdbcowplot

这是我的使用示例:

library(tidyverse)
library(extrafont)
library(cowplot)
library(palmerpenguins)
data(penguins)


penguins %>% 
  select(year, flipper_length_mm,species) %>% 
  ggplot(aes(x=year,y=flipper_length_mm,fill=species)) +
  geom_col() + 
  labs(title = "First Plot") + 
  theme(text = element_text(family = "Georgia")) -> plot1


penguins %>% 
  select(year, bill_length_mm,species) %>% 
  ggplot(aes(x=year,y=bill_length_mm,fill=species)) +
  geom_col() + 
  labs(title = "Second Plot") + 
  theme(text = element_text(family = "Georgia")) -> plot2

cowplot::plot_grid(plot1,plot2)

在此处输入图像描述

4

1 回答 1

0

感谢克劳斯威尔克在评论中的回答,很快就回答了这个问题:

需要设置空设备。 您可能必须安装开发版本才能完全正常工作(它对我来说确实工作正常!)。

简短的回答:

set_null_device(cairo_pdf)
cowplot::plot_grid(plot1,plot2)

并且错误消息消失。使用set_null_device("png")也对我有用,但鉴于我的目标是保存 PDF,根据克劳斯的说法,这是更安全的选择。

在全:

library(tidyverse)
library(extrafont)
library(cowplot)
library(palmerpenguins)
data(penguins)


penguins %>% 
  select(year, flipper_length_mm,species) %>% 
  ggplot(aes(x=year,y=flipper_length_mm,fill=species)) +
  geom_col() + 
  labs(title = "First Plot") + 
  theme(text = element_text(family = "Georgia")) -> plot1


penguins %>% 
  select(year, bill_length_mm,species) %>% 
  ggplot(aes(x=year,y=bill_length_mm,fill=species)) +
  geom_col() + 
  labs(title = "Second Plot") + 
  theme(text = element_text(family = "Georgia")) -> plot2


set_null_device(cairo_pdf)
cowplot::plot_grid(plot1,plot2)

于 2020-09-20T23:38:21.877 回答