【问题标题】:Using a font from extrafont in grid.draw在 grid.draw 中使用 extrafont 中的字体
【发布时间】:2017-05-15 18:34:59
【问题描述】:

假设我有一个这样的数据集:

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")

但是当我在情节上使用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 字体时,我没有收到错误消息。

感谢您的帮助!

【问题讨论】:

  • 试试"TradeGothic LT CondEighteen"
  • 有趣。这不是我试过的吗?有什么不同?
  • 你做了extrafont::loadfonts()吗?
  • 是的,我做到了。而且,我的意思是,字体有效。只有当我尝试使用 grid.draw() 时它才会中断。

标签: r ggplot2 fonts gtable


【解决方案1】:

我,呃,“刚好有”这个字体的副本,跳了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()ing 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(从预览导出为 PNG):

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(从预览导出为 PNG):

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 表示代码的各个位都可以选择正确的),但这就是 R 中字体的当前状态。

我为我的公司生成研究报告内容,并为屏幕(开发期间)和生产 PDF 生成(用于将最终输出移交给创意团队)单独的主题代码。这是一个令人沮丧的拐杖,但它有效。

【讨论】:

    猜你喜欢
    • 2018-02-12
    • 2019-08-24
    • 2020-07-23
    • 2016-07-25
    • 2021-12-21
    • 2021-11-17
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多