导出到pdf在ggplot2中无法正确显示 [英] Export to pdf not displaying properly in ggplot2

查看:191
本文介绍了导出到pdf在ggplot2中无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a complex figure P made up of figures Fig_NPK and Barchart_fert, they were made using a datasheet "Fert" with columns including "Vil", "N", "P", and "K". My goal is to create a pdf file and use ghostscript to embed the "Times New Roman" font family in the pdf file. When I use ggsave to export "P" to a pdf file it doesn't display properly. The code used were:

library(extrafont)
library(ggplot2)

人物,以Fig_N为例(Fig_NPK中的Fig_N,P和K)

Figures, Fig_N as an example (Fig_N, P, and K form 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 message after 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"导出到后记文件时,也会发生相同的情况.我一直在关注帮助文件和其他有关嵌入字体的答案,但似乎无法解决我的问题.

The resulting pdf file is incomplete. The same thing happens when I try exporting "P" to a postscript file. I have been following the help file and other answers on embedding fonts but can't seem to solve my issue.

推荐答案

在此 ggsave中的

确实解决了无法在pdf和tiff文件中显示正确字体的问题,尽管正如@ user1092247指出的那样,字距调整似乎很尴尬.如果有人能完善此解决方案,并进一步说明问题的实质,我们将不胜感激.

in ggsave does solve the problem of not display the correct font in pdf and tiff files, though as @user1092247 pointed out, the kerning seems awkward. Would still appreciate it if anyone could perfect this solution, and explain a bit more on what the problem actually was.

这篇关于导出到pdf在ggplot2中无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆