在grid.draw中使用extrafont中的字体

Jac*_*tis 6 fonts r ggplot2 gtable

假设我有这样的数据集:

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)
)
Run Code Online (Sandbox Code Playgroud)

我可以使用包装中的TradeGothic LT CondEighteen字体在ggplot中绘制它extrafonts:

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是当我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")
Run Code Online (Sandbox Code Playgroud)

运行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"...
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助!

hrb*_*str 7

我,呃,"碰巧有"这个字体的副本并做了extrafont::font_import()舞蹈并得到了你的错误.但是,经过进一步检查:

library(purrr)

loadfonts()

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

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

虽然R有很多令人敬畏的东西,但它处理字体的方式与蜥蜴乌鸦(#atla)一样细致入微,友好可用.

UPDATE

完整的例子(不破data.frame创建代码和引用plotVS p和实际grid.draw()荷兰国际集团gtVS plotp修改ggplot gtable后:

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")
Run Code Online (Sandbox Code Playgroud)

^^部分生成以下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()
Run Code Online (Sandbox Code Playgroud)

^^部分生成以下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)
Run Code Online (Sandbox Code Playgroud)

这是来自RStudio的屏幕抓取(包括RStudio UI位以显示它在RStudio图形设备中):

在此输入图像描述

令人遗憾的是我们必须这样做(更改字体的命名约定,以便R演示代码的各个位可以选择正确的一个)但这是R中字体的当前状态.

我为我公司的研究报告生成内容,并为屏幕(开发期间)和生产PDF生成(最终输出切换到创意团队)分别设置主题代码.这是一个令人沮丧的拐杖,但它的工作原理.