导出到pdf在ggplot2中无法正确显示 [英] Export to pdf not displaying properly in 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.