假设我有这样的数据集:
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 字体时,我不会收到错误。
感谢您的帮助!
答案 0 :(得分:7)
我,呃,&#34;恰巧有&#34;这个字体的副本,并extrafont::font_import()
跳舞,并得到你的错误。但是,经过进一步检查:
library(purrr)
loadfonts()
pdfFonts() %>%
map_chr("family") %>%
keep(~grepl(".*Trade.*", .))
## [1] "TradeGothic LT CondEighteen"
似乎PDF设备想要这个名字。
虽然R有很多令人敬畏的东西,但它处理字体的方式与蜥蜴乌鸦(#atla)一样细致入微,友好可用。
<强>更新强>
完整示例(没有损坏的data.frame
创建代码并引用plot
vs p
,实际grid.draw()
ing gt
vs plot
或{修改ggplot gtable后的{1}}:
p
^^部分生成以下PDF(从预览中导出为PNG):
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()
这里是来自RStudio的屏幕抓取(包括RStudio UI位以显示它在RStudio图形设备中):
很遗憾我们必须这样做(更改字体的命名约定,以便R演示代码的各个位可以选择正确的字体),但这是R中字体的当前状态。 / p>
我为公司的研究报告生成内容,并为屏幕(开发期间)和生产PDF生成(用于创意团队的最终输出切换)分别设置主题代码。这是一个令人沮丧的拐杖,但它确实有效。