R语言作图保存在pdf中时无法显示中文标题

有两个方法可以实现:

1、直接在pdf( )这个函数中设置family这个参数,进行字体设置

但是字体单一,只有标准宋体,如:

 > pdf("d:/def.pdf",family="GB1")
 > plot(x,y,xlab="高度",ylab="体重",main="统计")
 > dev.off() 

2、调用 Cairo程序包,使用CairoPDF( )函数

> library(Cairo)
> CairoPDF("d:/def.pdf")
> plot(x,y,xlab="高度",ylab="体重",main="统计",family="STKaiti")
> dev.off()


如果不知道family参数设置成什么,可以写一个函数来定义,如:

FontsCE<-function(x){
FC<-c( "新细明体","细明体","标楷体","黑体","宋体","新宋体","仿宋","楷体","仿宋_GB2312","楷体_GB2312","微软正黑体","微软雅黑", "隶书","幼圆","华文细黑","华文楷体","华文宋体","华文中宋", "华文仿宋","方正舒体" ,"方正姚体","华文彩云","华文琥珀","华文隶书", "华文行楷","华文新魏" ) 

 FE<-c( "PMingLiU" ,"MingLiU","DFKai-SB","SimHei","SimSun" ,"NSimSun","FangSong","KaiTi","FangSong_GB2312","KaiTi_GB2312","Microsoft JhengHei","Microsoft YaHei","LiSu","YouYuan","STXihei","STKaiti","STSong","STZhongsong","STFangsong","FZShuTi","FZYaoti","STCaiyun","STHupo","STLiti","STXingkai","STXinwei")

CFonts<-data.frame(FC,FE,stringsAsFactors=F) 

n<-which(CFonts==x)
result<-CFonts[n,2]
return(result)

}


这样family可以设置为family=FontsCE("华文楷体")等等

你可能感兴趣的:(R)