R postscript()中文

时间:2013-04-24 04:15:08

标签: r postscript

我正在尝试设置R postscript()设备来显示中文字符,但没有成功。

我正在使用带有以下R sessionInfo()信息的Macbook Pro Mountain Lion 10.8.3:


R version 3.0.0 (2013-04-03) 
Platform: x86_64-apple-darwin10.8.0 (64-bit) 

locale: 
[1] en_US.UTF-8 

attached base packages: 
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached): 
[1] tools_3.0.0 

以下是我的主要代码

postscript("圖9-35plotmath.ps", horizontal = FALSE, height = 6) 
plot(c(-pi, -pi/2, 0, pi/2, pi), 1:5, type = "n", xaxt = "n", main = expression(paste(plain(sin) * phi, "與", plain(cos) * phi)), ylab = expression("sin" * phi), xlab = expression(paste("Phase Angle", phi))) 

axis(side = 1, at = c(-pi, -pi/2, 0, pi/2, pi), labels = expression(-pi, -pi/2, 0, pi/2, pi)) 

for(i in 1:5) 
{ 
    text(-2.5, i, substitute(list(xi, eta) == group("(", list(x, y), ")"), list(x = i, y = i+1))) 
} 

text(-1.7, 5, expression("一階微分"== {f * minute}(x)), adj = 0) 
text(-1.7, 4, expression("二階微分"== {f * second}(x)), adj = 0) 
text(-1.7, 3, pos = 4, expression(hat(beta) == (X^t * X)^{-1} * X^t *y)) 
text(-1.7, 2, pos = 4, expression(bar(x) == sum(frac(x[i], n), i == 1, n))) 
text(-1.7, 1.2, pos = 4, expression(paste(frac(1, sigma*sqrt(2*pi)), " ", plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})), cex = 1.2) 

text(0.5, 4.6, pos = 4, expression(prod(plain(P)(X == k), k = 1, n))) 
text(0.5, 4, pos = 4, expression(integral(f(x)*dx, a,b))) 
text(0.5, 3, pos = 4, expression(union(A[i], i  == 1, n))) 
text(0.5, 2, pos = 4, expression(intersect(A[i], i  == 1, n))) 
text(0.5, 1, pos = 4, expression(lim(f(x), x %->% 0))) 

text(2, 4.5, pos = 4, expression(min(f(x), x > 0))) 
text(1, 3.5, pos = 4, expression(Y == beta[0] + list(beta[1]*X[1], ..., beta[p-1]*X[p-1]))) 
text(1.5, 2.5, pos = 4, expression(S^2 == sqrt(frac(sum((X[i]=bar(x))^2), n-1)))) 
dev.off() 

以上内容无效,所得到的postscript文件为所有这些中文字符提供了内容。然后,我尝试了以下内容:

在postscript()中使用现有的GB1字体。它给了我一个.ps文件,无法通过Mac预览,Ghostscript和Photoshop打开。

postscript("圖9-35plotmath.ps", horizontal = FALSE, height = 6, fonts=c("GB1")) 
plot(c(-pi, -pi/2, 0, pi/2, pi), 1:5, type = "n", xaxt = "n", family = "GB1", main = expression(paste(plain(sin) * phi, "與", plain(cos) * phi)), ylab = expression("sin" * phi), xlab = expression(paste("Phase Angle", phi))) 

...............(omitted) 
text(-1.7, 5, family = "GB1", expression("一階微分"== {f * minute}(x)), adj = 0) 
text(-1.7, 4, family = "GB1", expression("二階微分"== {f * second}(x)), adj = 0) 
..............(omitted) 

我也尝试使用CIDFont()添加一些字体,但它仍然提供了一个根本无法打开的.ps文件。

song = CIDFont("SimSun", "GBK-EUC-H", "GBK", "") 
postscriptFonts(song = song) 
postscript("圖9-35plotmath.ps", horizontal = FALSE, height = 6, family="song") 
plot(c(-pi, -pi/2, 0, pi/2, pi), 1:5, type = "n", xaxt = "n", family = "song", main = expression(paste(plain(sin) * phi, "與", plain(cos) * phi)), ylab = expression("sin" * phi), xlab = expression(paste("Phase Angle", phi))) 

..............(omitted) 

text(-1.7, 5, family = "song", expression("一階微分"== {f * minute}(x)), adj = 0) 
text(-1.7, 4, family = "song", expression("二階微分"== {f * second}(x)), adj = 0) 
.............(omitted) 

两种方法都没有警告,但无法打开.ps文件。

我发现很难理解postscript()中的字体和系列定义,其中par(family =“SimSun”)完全没有问题显示并保存pdf()和png()设备中的以下内容。

我需要制作一个用于高质量打印的postscript文件。

再次感谢。

SS

2 个答案:

答案 0 :(得分:1)

这就是Paul Murrell和Brian Ripley在2006 R-News article:

中的表现
 pdf("chinese.pdf", width=3, height=1) 
 grid.text("\u4F60\u597D", y=2/3,
            gp=gpar(fontfamily="CNS1")) 
 grid.text("is 'hello' in (Traditional) Chinese",  y=1/3) 
 dev.off()

enter image description here

仅仅是为了记录,drammock在Mac 10.6.8上以R 3.0.0运行的代码在MacGhostView中打开时生成:

enter image description here

编辑:您可以使用以下方法检查postscript设备可用的字体:

names(postscriptFonts())

答案 1 :(得分:0)

cairo_ps()适用于Mac,包括基本图形:

cairo_ps(filename="foo.eps", family="LiSong Pro")
plot.new()
text(0.5, 0.5, "這是漢字")
dev.off()

...和网格:

library(grid)
cairo_ps(filename="foo.eps", family="LiSong Pro")
plot.new()
grid.text("這是漢字")
dev.off()