[英]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:到目前为止我尝试过的:
extrafont::font_install()
extrafont::font_install()
安装字体extrafont::loadfonts()
extrafont::loadfonts()
加载字体I have extrafont
and extrafontdb
as well as cowplot
installed.我安装了
extrafont
和extrafontdb
以及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!).
您可能必须安装开发版本才能完全正常工作(它对我来说确实很好用!)。
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 的说法,这是更安全的选择。
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.