说我有一个像这样的数据集:

dat <- data.frame
  text = c(
    "It made me feel very positive to brand X",
    "It was clear and easy to understand",
    "I didn't like it al all"),
  value=runif(3)
)

我可以使用extrafonts包中的TradeGothic LT CondEighteen字体将其绘制在ggplot中:
library(ggplot2)
p <- ggplot(dat, aes(text, value)) +
     geom_bar(stat="identity") +
     coord_flip() +
     labs(title="     Do you agree with the following statements?")+
     theme_bw(16)+
     theme(text=element_text(family="TradeGothic LT CondEighteen"))

ggsave('plot.pdf', plot = plot,  path = "/Users/jacobdeecurtis/Desktop")

r - 在grid.draw中使用Extrafont中的字体-LMLPHP

但是当我在情节上使用ggplot_gtable时:
gt <- ggplot_gtable(ggplot_build(plot))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1,         max(gt$layout$r))
grid::grid.draw(plot)

ggsave('plot.pdf', plot = plot,  path = "/Users/jacobdeecurtis/Desktop")

运行grid.draw函数后出现错误。错误是:
Error in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  polygon edge not found
In addition: Warning messages:
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  :
  no font could be found for family "TradeGothic LT CondEighteen"...

当我不使用TradeGothic LT CondEighteen字体时,我没有收到错误消息。

谢谢你的帮助!

最佳答案

我,“恰好拥有”该字体的副本,然后extrafont::font_import()跳了起来,并得到了您的错误。但是,经过进一步检查:

library(purrr)

loadfonts()

pdfFonts() %>%
  map_chr("family") %>%
  keep(~grepl(".*Trade.*", .))
## [1] "TradeGothic LT CondEighteen"

看来PDF设备想要这个名字。

尽管R有很多很棒的功能,但是它处理字体的方式与蜥蜴乌鸦(#atla)一样微妙,友好且可用。

更新

完整示例(修改了ggplot gtable后,没有损坏的data.frame创建代码和引用plotp的引用,实际上是grid.draw()gtplotp的引用:
library(ggplot2)
library(grid)
library(extrafont)

dat <- data.frame(
  text = c(
    "It made me feel very positive to brand X",
    "It was clear and easy to understand",
    "I didn't like it al all"),
  value=runif(3)
)

p <- ggplot(dat, aes(text, value)) +
  geom_bar(stat="identity") +
  coord_flip() +
  labs(title="     Do you agree with the following statements?")+
  theme_bw(16)+
  theme(text=element_text(family="TradeGothic LT CondEighteen"))

loadfonts()

ggsave('plot.pdf', plot=p,  path="~/Desktop")

该^^部分生成以下PDF(从Preview导出为PNG):

r - 在grid.draw中使用Extrafont中的字体-LMLPHP
gt <- ggplot_gtable(ggplot_build(p))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r))

pdf("~/Desktop/plot.pdf")
grid::grid.draw(gt)
dev.off()

该^^部分生成以下PDF(从Preview导出为PNG):

r - 在grid.draw中使用Extrafont中的字体-LMLPHP
p <- ggplot(dat, aes(text, value)) +
  geom_bar(stat="identity") +
  coord_flip() +
  labs(title="     Do you agree with the following statements?")+
  theme_bw(16)+
  theme(text=element_text(family="TradeGothicLT-CondEighteen"))

gt <- ggplot_gtable(ggplot_build(p))
gt$layout[which(gt$layout$name == "title"), c("l", "r")] <- c(1, max(gt$layout$r))

grid::grid.draw(gt)

这是RStudio的屏幕抓图(包括一些RStudio UI,以显示它在RStudio图形设备中):

r - 在grid.draw中使用Extrafont中的字体-LMLPHP

可悲的是我们必须这样做(更改字体的命名约定,以便R表示代码的各个位都可以选择正确的字体),但这就是R中字体的当前状态。

我为公司的研究报告生成内容,并为屏幕(在开发过程中)和生产PDF(为最终输出移交给创意团队)提供单独的主题代码。这是一个令人沮丧的拐杖,但它可以工作。

关于r - 在grid.draw中使用Extrafont中的字体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41413508/

10-12 17:10
查看更多