简体   繁体   English

将“extrafont”与“cowplot”一起使用:字体宽度未知,字符错误

[英]Using `extrafont` with `cowplot`: font width unknown for character error

I'm trying to use a font in ggplot that I can only get through the extrafont package.我正在尝试在ggplot中使用一种只能通过extrafont包获得的extrafont When I then want to combine multiple plots using the cowplot package, I always a large number of errors of the sort:然后,当我想使用cowplot包组合多个图时,我总是会出现大量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

Note the package does actually produce the output (ie a plot side-by-side) but the error messages concern me.请注意,该包确实产生了输出(即并排的绘图),但错误消息与我有关。

What I tried so far:到目前为止我尝试过的:

  • Installing the fonts using extrafont::font_install()使用extrafont::font_install()安装字体
  • Loading the fonts using extrafont::loadfonts()使用extrafont::loadfonts()加载字体

I have extrafont and extrafontdb as well as cowplot installed.我安装了extrafontextrafontdb以及cowplot

Here an example of my use:这是我的使用示例:

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)

在此处输入图片说明

Just quickly answering this thanks to Claus Wilke's answer in the comments:感谢 Claus Wilke 在评论中的回答,快速回答了这个问题:

It is necessary to set the null device.需要设置空设备。 You may have to install the development version for this to fully work (it did work fine for me!).您可能必须安装开发版本才能完全正常工作(它对我来说确实很好用!)。

The short answer:简短的回答:

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

And the error messages disappear.并且错误消息消失。 Using set_null_device("png") also worked for me, but given that I'm aiming to save a PDF, this is the safer option according to Claus.使用set_null_device("png")也对我set_null_device("png") ,但鉴于我的目标是保存 PDF,根据 Claus 的说法,这是更安全的选择。

In full:在全:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM