ggplot嵌入式字体在pdf

ggplot嵌入式字体在pdf

本文介绍了ggplot嵌入式字体在pdf中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用下面的指南将 ggplot 制成的图表导出为pdf格式:



它提出了一些字体的底部不应该出现的问题,这发生在我的例子下面。显示正确,而在Calibri中的文本显示正确。



有没有人找到解决这个问题的方法?

  library(ggplot2)
library(plyr)
library(grid)
library(gridExtra)
library extrafont)

data1< -as.data.frame(1:5)
data1 [,2]< -as.data.frame(c(1,3,5,7 ,9))
data1 [,3]< -as.data.frame(c(2,4,6,8,10))
colnames(data1)< -c(x (y = y1,color =Taux selon DEF),y1,y2)

ggplot(data1,aes(x = x))+
geom_line ,size = 0.61,color =black)+
geom_line(aes(y = y2,color =Taux selon EC),size = 0.61,color =black,linetype =dashed)+
xlab(X axis lab)+ ylab(Y axis lab))+
annotate(text,x = 1,y = 4,label =这里有一些文字 = 2,family =Bauhaus 93)+
注释(text,x = 4,y = 1,label =这里有更多文字,size = 2,family =Calibri)+
theme_bw()+ theme(legend.title = element_blank(),
legend.key = element_rect(fill = NA),
panel.border = element_blank(),
axis.line = element_line(color =black,size = 0.25),
轴。 ticks = element_line(size = 0.25),
panel.grid.major = element_line(color =grey80,size = 0.25),
panel.grid.minor = element_line(color =grey80, size = 0.25),
axis.text.x = element_text(size = 5.5,lineheight = 0.9,hjust = 0.5,family =Bauhaus 93),
axis.text.y = element_text(size = 5.5,lineheight = 0.9,vjust = 0.5,family =Calibri),
axis.title.y = element_text(size = 6.1,angle = 0,vjust = 0.975,face =bold Calibri),
axis.title.x = element_text(size = 6.1,angle = 0,vjust = -0.20,face =bold,family =Calibri))+
scale_x_continuous expand = c(0,0),limits = c(0,5))+
scale_y_continuous(expand = c(0,0),limits = c(0,10))+
ggtitle(Title)+
ggsave(Test.pdf,width = 7,height = 5)
Sys.setenv(R_GSCMD = C:/ Program Files(x86)/PDF24/gs/bin/gswin32.exe)
embed_fonts(Test.pdf)

尝试向 ggsave()添加 device = cairo_pdf 呼叫。这个出现来解决我的问题。这样,不再需要使用 embed_fonts()



请参阅 mgaudet 这里的评论:


I have been using the following guide to export plots made with ggplotto pdf: plot fonts guide

It raises the issue at the bottom of the post of some fonts not appearing as they should, which happens in my example below. The text in font Bauhaus 93 appears correctly whilst the text in Calibri is displayed incorrectly.

Has anyone found a way to resolve this issue?

library(ggplot2)
library(plyr)
library(grid)
library(gridExtra)
library(extrafont)

data1<-as.data.frame(1:5)
data1[,2]<-as.data.frame(c(1,3,5,7,9))
data1[,3]<-as.data.frame(c(2,4,6,8,10))
colnames(data1)<-c("x","y1","y2")

ggplot(data1, aes(x=x)) +
  geom_line(aes(y = y1, colour = "Taux selon DEF"), size=0.61, colour="black") +
  geom_line(aes(y = y2, colour = "Taux selon EC"), size=0.61, colour="black", linetype="dashed") +
  xlab("X axis lab") + ylab("Y axis lab)") +
  annotate("text", x=1, y=4, label="Some text here", size=2, family="Bauhaus 93") +
  annotate("text", x=4, y=1, label="More text here", size=2, family="Calibri") +
  theme_bw() + theme(legend.title = element_blank(),
                     legend.key = element_rect(fill=NA),
                     panel.border = element_blank(),
                     axis.line = element_line(colour="black", size=0.25),
                     axis.ticks = element_line(size=0.25),
                     panel.grid.major = element_line(colour = "grey80", size=0.25),
                     panel.grid.minor = element_line(colour = "grey80", size=0.25),
                     axis.text.x = element_text(size=5.5 , lineheight=0.9, hjust=0.5, family="Bauhaus 93"),
                     axis.text.y = element_text(size=5.5 , lineheight=0.9, vjust=0.5, family="Calibri"),
                     axis.title.y = element_text(size=6.1, angle=0, vjust=0.975, face="bold", family="Calibri"),
                     axis.title.x = element_text(size=6.1, angle=0, vjust=-0.20, face="bold", family="Calibri")) +
  scale_x_continuous(expand = c(0, 0), limits=c(0,5)) +
  scale_y_continuous(expand = c(0, 0), limits=c(0,10)) +
  ggtitle("Title") +
  ggsave("Test.pdf", width=7, height=5)
Sys.setenv(R_GSCMD = "C:/Program Files (x86)/PDF24/gs/bin/gswin32.exe")
embed_fonts("Test.pdf")
解决方案

Try adding device=cairo_pdf to the ggsave() call. This appears to solve the problem for me. This way, it's no longer necessary to use embed_fonts().

See mgaudet's comment here: https://github.com/wch/extrafont/issues/8

这篇关于ggplot嵌入式字体在pdf中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:01