1
1

Rで解像度の高い図を出力するためのplot()の設定

Last updated at Posted at 2024-08-22

Rでjpegtiffといった関数をデフォルトのままの設定で使うと、解像度のあまり高くない図が出力されます。自分で確認する用にはいいとしても、論文など正式な場に向けた図としてはもう少し解像度を上げたいところです。

x <- 1:10
y <- x + rnorm(10)
lmm <- lm(y ~ x)

jpeg('Fig1.tiff')
plot(x, y, main = 'Title')
abline(coef(lmm))
dev.off()

Fig1.tiff

しかし、図の出力サイズだけを大きいものにすると、相対的に点や文字が小さくなってしまいます。

jpeg('Fig2.tiff', width = 2000, height = 2000)
plot(x, y, main = 'Title')
abline(coef(lmm))
dev.off()
Fig2.tiff

体裁を整えるため、パラメータを色々と設定しました。誰かの役に立つかもしれませんので、結果を載せておきます。

# mgp (margin gap?) はプロットエリア(プロットを囲う四角)と
# 軸タイトル・軸ラベル・軸との距離を表す。
# 複数回登場するので、事前に指定しておく。
mgp_x <- c(15, 2.5, 0) # x軸の設定
mgp_y <- c(12, 2.5, 0) # y軸の設定

jpeg('Fig3.tiff', width = 2000, height = 2000)

par(mar = c(20, 20, 20, 5)) # プロットエリア周囲の余白の大きさ(下, 左, 上, 右)

plot(x, y, 
     cex  = 10,      # 点の大きさ(character extention/expansion)
     lwd  = 3,       # 点を描く線の太さ(line width)
     xaxt = 'n',     # x軸を描かない(x axis type)
     yaxt = 'n',     # y軸を描かない(y axis type)
     ann  = FALSE)   # 軸タイトルを描かない(annotation)
abline(coef(lmm), 
       lwd = 5)      # 線の太さ

axis(side      = 1,  # プロットエリアの下に軸を描く
     cex.axis  = 7,  # 軸ラベルの大きさ
     padj      = 1,  # 軸ラベルを描く位置の基準点を、文字の上端に指定
     tcl       = -2, # 軸目盛の長さ(正の値だと目盛がプロットエリア内に伸びる)
     lwd.ticks = 4,  # 軸目盛の太さ
     mgp       = mgp_x)
axis(side      = 2,    
     cex.axis  = 7, 
     tcl       = -2, 
     lwd.ticks = 4, 
     mgp       = mgp_y)

title(xlab     = 'x', 
      cex.lab  = 9,   # 軸ラベルの文字サイズ
      mgp      = mgp_x)
title(ylab     = 'y', 
      cex.lab  = 9, 
      mgp      = mgp_y)
title(main     = 'Title', 
      cex.main = 9)   # タイトルの文字サイズ

box(lwd = 4) # プロットエリアを囲む四角の線の太さ

dev.off()
Fig3.tiff
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1