[R] can't print ggplot with Chinese characters to pdf files

John miaojpm at gmail.com
Thu Oct 12 15:12:49 CEST 2017


I install the Chinese font "Kaiti TC" on my mac, but I can't print the
figures to pdf file by "marrangeGrob" command, which is in the package
"gridExtra". Error message after I type "ggsave(......)" (last line of the
program):

"Saving 7.47 x 5.15 in image
Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  :
  invalid font type
In addition: There were 50 or more warnings (use warnings() to see the
first 50)"

How can I install the font so that I can see the figures on the pdf files?
Some information that may be useful:
(1) If I use the English ggtitle, there is no problem at all. The pdf file
present the figures perfectly.
(2) If I use Chinese title, I can still see the figures p1 and p2 on
Studio, but I can't see it at the pdf file produced by ggsave. It gives an
error message



rm(list=ls())
library(ggplot2)
library(gridExtra)
df1<-data.frame(x=1:2, y=3:4, z=5:6)
#p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test1")
#p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("test2")
p1<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("測試")  #Chinese title
p2<-ggplot(df1, aes(x=x, y=y))+geom_line()+ggtitle("中文")  #Chinese title
p1<-p1+theme(text = element_text(family = "Kaiti TC"))
p2<-p2+theme(text = element_text(family = "Kaiti TC"))

p<-array(list(NA), dim=2)
p[[1]]<-p1
p[[2]]<-p2
p_series <- lapply(1:(length(p)), function(.x) p[.x][[1]])
m2 <- marrangeGrob(p_series, nrow=2, ncol=1)
ggsave("test_plot_chinese.pdf", m2)

	[[alternative HTML version deleted]]



More information about the R-help mailing list