gpt4 book ai didi

r - 导出为 pdf 在 ggplot2 中无法正确显示

转载 作者:行者123 更新时间:2023-12-05 02:19:54 34 4
gpt4 key购买 nike

我有一个复杂的图形 P,由图形 Fig_NPK 和 Barchart_fert 组成,它们是使用数据表“Fert”制作的,列包括“Vil”、“N”、“P”和“K”。我的目标是创建一个 pdf 文件并使用 ghostscript 在 pdf 文件中嵌入“Times New Roman”字体系列。当我使用 ggsave 将“P”导出到 pdf 文件时,它无法正确显示。使用的代码是:

library(extrafont)
library(ggplot2)

图,以Fig_N为例(Fig_N、P、K组成Fig_NPK)

Fig_N<-ggplot(aes(y = N, x = factor(Vil)), data = Total_fac[which(Total_fac$N<quantile(Total_fac$N,0.95)&Total_fac$N>quantile(Total_fac$N,0.05)),])+stat_boxplot(geom ="errorbar") + geom_boxplot() +ggtitle("N requirement")+labs(x="Village",y="Amount used (kg)") +theme(text=element_text(family="Times New Roman", face="plain", size=14))
Fig_NPK<-plot_grid(Fig_N,Fig_P,Fig_K, nrow=3,align = "v")
Barchart_fert<-ggplot(Fert, aes(x=Village, y=Amount, fill=Fertilizer)) + geom_bar(stat = "identity", width=0.4)+ggtitle("Fertilizer usage")+ylab("Amount used (kg)")+theme(axis.text.x=element_text(vjust = 0.5))+scale_fill_discrete(name="Fertilizer type", breaks=c("N-Eq.", "P2O5-Eq.", "K2O-Eq."),labels=c("N Eq.", expression('P'[2]*'O'[5]~~Eq.), expression('K'[2]*'O'~~Eq.)), c=60, l=80)+theme(text=element_text(family="Times New Roman", face="plain", size=14))
P<-ggdraw() + draw_plot(Fig_NPK,0,0,.4,1)+draw_plot(Barchart_fert,.4,0,.6,1) + draw_plot_label(c("A", "B"), c(0,.4), c(1,1), size=14)
ggsave("FigP.pdf", plot=P, width=5, height=5)

ggsave 后的错误信息:

Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  : Metric information not available for this family/device In addition: There were 50 or more warnings (use warnings() to see the first 50)

生成的 pdf 文件不完整。当我尝试将“P”导出到 postscript 文件时,也会发生同样的事情。我一直在关注有关嵌入字体的帮助文件和其他答案,但似乎无法解决我的问题。

最佳答案

在此 answer 中找到解决方案.添加

device=cairo_pdf

在 ggsave 中确实解决了在 pdf 和 tiff 文件中不显示正确字体的问题,尽管正如@user1092247 指出的那样,字距调整似乎很尴尬。如果有人可以完善此解决方案,并进一步解释问题的实际原因,我们将不胜感激。

关于r - 导出为 pdf 在 ggplot2 中无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826684/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com