// 谷村晋
// 自由に加筆をお願いします。

COLOR(green){SIZE(25){EPS形式の出力}}

ここではグラフィックウィンドウに描画されているグラフを EPS 形式で出力する際のオプションの実際を紹介します。eps ファイルの加工方法については[[こちら:http://www.okada.jp.org/RWiki/?cmd=read&page=Windows%20%A4%CB%A4%E8%A4%EB%20eps%20%A5%D5%A5%A1%A5%A4%A5%EB%CA%D4%BD%B8%28ad%2Abe%A5%BD%A5%D5%A5%C8%A4%CB%CD%EA%A4%E9%A4%CA%A4%A4%CA%FD%CB%A1%29]]。作成したepsファイルをLaTeXに取り込む方法は、[[こちら:http://www.okada.jp.org/RWiki/index.php?cmd=read&page=R%A4%CB%A4%E8%A4%EB%A5%DD%A5%B9%A5%C8%A5%B9%A5%AF%A5%EA%A5%D7%A5%C8%B2%E8%C1%FC%A4%CELaTeX%A4%C7%A4%CE%CD%F8%CD%D1&word=postscript]]

#contents
#comment

* EPS形式の出力コマンド [#zfb2be64]
 > dev.copy2eps(file="ファイル名.eps")
表示されているグラフィックが EPSF 形式で保存されます。dev.copy2epsは内部的に、
 > dev.copy(device=postscript,file="ファイル名.eps", onefile=FALSE,horizontal=FALSE,paper=special)
と同じことをしている。いちいちオプションを指定するのは面倒なので、dev.copy2epsを使った方が楽。

* 出力サイズ指定 [#h2c110de]
 > dev.copy2eps(file="ファイル名.eps",width=数値,height=数値)
幅や高さを指定できる。幅と高さのどちらか一方のみを指定すると、コピー元のアスペクト比で他方を自動的に決定してくれる。指定する数値の単位はインチ。~
''例'' 横幅を4インチにする場合
 > dev.copy2eps(file="width-test.eps",width=4)

* フォント指定 [#abe46caf]
 > dev.copy2eps(file="ファイル名.eps",family="フォント名")
** 指定できるフォント(オリジナル版R) [#laa3b60c]
| 標準ポストスクリプトフォント | AvantGarde |
|~| Bookman |
|~| Courier |
|~| Helvetica |
|~| Helvetica-Narrow |
|~| NewCenturySchoolbook |
|~| Palatino |
|~| Times |
| URWフォント | URWGothic |
|~| URWBookman |
|~| NimbusMon |
|~| NimbusSan |
|~| NimbusSanCond |
|~| CenturySch |
|~| URWPalladio |
|~| NimbusRom |
| その他 | ComputerModern |

// - ポストスクリプトフォント(AvantGarde, Bookman, Courier, Helvetica,  Helvetica-Narrow, NewCenturySchoolbook, Palatino, Times)
// - URWフォント(URWGothic, URWBookman, NimbusMon, NimbusSan, NimbusSanCond,      CenturySch, URWPalladio, NimbusRom)
// - その他(ComputerModern)
** 表示例 [#z18e9114]
 epsfont <- function(x) {
   plot(0:2,0:2,type="n",axes=FALSE,xlab="",ylab="")
   text(0,1.9,x,cex=5,font=1,pos=4) #標準
   text(0,1.6,x,cex=5,font=2,pos=4) #太字
   text(0,1.3,x,cex=5,font=3,pos=4) #斜体
   text(0,1,x,cex=5,font=4,pos=4)   #太字斜体
   text(0,0.7,"0123456789",cex=3,pos=4)
   dev.copy2eps(file=paste(x,".eps",sep=''),family=x)
   system(paste("convert -trim ",x,".eps ",x,".png",sep='')) #ImageMagickのtrimオプションを使う
 }
このepsfont()を使って表示してみる。

*** AvantGarde [#g6adbafb]
 > epsfont("AvantGarde")
#ref(AvantGarde.png,center)
*** Bookman [#x0a35f00]
 > epsfont("Bookman")
#ref(Bookman.png,center)
*** Courier [#i2b44c79]
 > epsfont("Courier")
#ref(Courier.png,center)
*** Helvetica [#d9dc3786]
familyを無指定の時に適用されるデフォルトのフォント
 > epsfont("Helvetica")
#ref(Helvetica.png,center)
*** Helvetica-Narrow [#o16f21a6]
 > epsfont("Helvetica-Narrow")
#ref(Helvetica-Narrow.png,center)
*** NewCenturySchoolbook [#f2a2657f]
 > epsfont("NewCenturySchoolbook")
#ref(NewCenturySchoolbook.png,center)
*** Palatino [#r97a14c8]
 > epsfont("Palatino")
#ref(Palatino.png,center)
*** Times [#add19e1d]
 > epsfont("Times")
#ref(Times.png,center)
*** URWGothic [#a7a40e88]
 > epsfont("URWGothic")
#ref("URWGothic.png",center)
*** URWBookman [#ofa68c25]
 > epsfont("URWBookman")
#ref("URWBookman.png",center)
*** NimbusMon [#l6de88b7]
 > epsfont("NimbusMon")
#ref("NimbusMon.png",center)
*** NimbusSan [#d0695586]
 > epsfont("NimbusSan")
#ref("NimbusSan.png",center)
*** NimbusSanCond [#cc1121cc]
 > epsfont("NimbusSanCond")
#ref("NimbusSanCond.png",center)
*** CenturySch [#p67baca2]
 > epsfont("CenturySch")
#ref("CenturySch.png",center)
*** URWPalladio [#bf41bca4]
 > epsfont("URWPalladio")
#ref("URWPalladio.png",center)
*** NimbusRom [#ve8b4b0b]
 > epsfont("NimbusRom")
#ref("NimbusRom.png",center)
*** ComputerModern [#u2cb3246]
 > epsfont("ComputerModern")
#ref("ComputerModern.png",center)

* pointsizeの指定 [#w1107c99]
点の大きさを指定できる
** pointsizeの指定例 [#eaebf640]
#ref(pointsize.png,center)
何故だがpointsizeを大きくすると描画領域が小さくなる。
* titleの指定 [#x62d8898]
これはグラフィックのmainオプションとは異なり、EPSファイルの中に書き込まれるタイトル。例えば、
 > dev.copy2eps(title="ADIS mortality in Honduras")
として、出力されたEPSファイルの中を見ると
 %!PS-Adobe-3.0 EPSF-3.0
 %%DocumentNeededResources: font Helvetica
 %%+ font Helvetica-Bold
 %%+ font Helvetica-Oblique
 %%+ font Helvetica-BoldOblique
 %%+ font Symbol
 %%Title: ADIS mortality in Honduras
 %%Creator: R Software
 %%Pages: (atend)
 %%BoundingBox: 0 0 268 263
 %%EndComments
となっている。

* カラーのグラフィックをグレー階調のepsにする [#r1ffdbfd]
簡単にグレー階調になってくれないグラフィック関数を使っているとき、グレー階調に指定し直すのに手間がかかるとき、または、カラーとグレー階調のepsを両方とも同時に欲しいとき、dev.copy2epsのfileにパイプを書いて、外部プログラムに処理を渡します。

カラーのepsをグレー階調にする[[bw_convert:http://www-hermes.desy.de/notes/extras/bw_convert]]というPerlスクリプトがありますが、これは引数に変換元ファイル名を与える必要があり、パイプに対応していません。パイプに対応していないと、dev.copy2eps()からは直接利用できないため、bw_convertのアイデアを拝借して、sedで対応します。

 > pie(c(2,10,3),c("S","R","S-plus"))
 > dev.copy2eps(file="カラー.eps")
 > dev.copy2eps(file="| sed -e 's/\bsetrgbcolor\b/add add 3 div setgray/g' > グレー階調.eps")

&ref("color-pie.png"); &ref("gray-pie.png");

また、ImageMagickのconvertを使う方法もあります。
> dev.copy2eps(file="| convert -type Grayscale - グレー階調.eps")
 > dev.copy2eps(file="| convert -type Grayscale - グレー階調.eps")

Rによるeps出力は、フォントを埋め込まずベクター形式で、大変軽いファイルになっています。しかし、convertで変換すると、ラスター化されるので、見栄えは似ていますが、ファイルサイズがときに100倍近くにふくれあがるので注意が必要です。

* 暗色のスライド用に色を変更する [#e11bd97b]
プレゼンテーション用に色を反転させたグラフィックも同時に欲しくなるかもしれません。グレー階調化と同様にsedでepsファイルを編集します。

ここでは、昔懐かしいブルースライドにしてみます。
 
  > par(bg='white') #背景色設定をepsに入れないと、そもそも背景色の変更ができない
  > pie(c(2,10,3),c("S","R","S-plus"))
  > dev.copy2eps(file="カラー.eps")
  > blue.slide <- paste("| sed -e 's/\\bsetrgbcolor\\b/pop pop pop 0 0 0.6  setrgbcolor/g'",
  +   "-e 's/\\b1 setgray\\b/0 0 0.6 setrgbcolor/g'",
  +   "-e 's/\\b0 setgray\\b/1 setgray/g' > スライド.eps") #長いのでpaste()を使う
  > dev.copy2eps(file=blue.slide)

&ref("color-pie.png"); &ref("blue-pie.png");

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS