データセット一覧の作図
アヤメ (iris) という花のデータセットを使って、plot()という呪文の説明をします。
まず、dataという呪文を唱えて、アヤメのデータセットを呼び出しましょう。
data(iris)
上の呪文だけでは「コンソール画面で何も起こっていないから不安」という人もいると思います。そこで、
iris
と唱えて、ちゃんとirisが呼び出されていることを確かめましょう。
ちゃんと150個分の花のデータが出力されましたね。 #でも、このままでは、ちょっと使い勝手が悪いので、attachという呪文で、このデータセットを、あなたのRに「装着」しましょう。
attach(iris)
呪文plotを使って、このデータセットの全体像を描き出しましょう。
plot(iris)
新たな画面が開いて、データセットの各データ間の散布図が描き出されましたね。
この作図法は、複数種類のデータがまとめられたデータファイルの全体像を把握するため、また、データの中に誤入力による外れ値がないかをチェックするために役立ちます

散布図
今度は呪文plotで2種類の量的データの関係を表す散布図を描きましょう。plotには色々な使い方がありますが、散布図と折れ線グラフが代表的な使い方だと思います。
入力方法にはplot(y ~ x)という方法と、plot(x, y)という方法があります。
plot(Petal.Length ~ Sepal.Length)
plot(Petal.Length, Sepal.Length)
X軸とY軸が入れ替わりましたね。

少し詳しい設定方法
軸の設定

軸の上限と下限はxlimやylimという引数で指定します。
plot(Petal.Length ~ Sepal.Length, xlim=c(0, 10))
plot(Petal.Length ~ Sepal.Length, ylim=c(0, 10))

点の形
散布図の点の形はpchという引数で設定します。デフォルトは1なので、1以外としてみましょう。
plot(Petal.Length ~ Sepal.Length, pch=0) #0だと四角。1と同じで中抜きです。
plot(Petal.Length ~ Sepal.Length, pch=2) #2だと三角。1と同じで中抜きです。
plot(Petal.Length ~ Sepal.Length, pch=3) #3だとプラス。
plot(Petal.Length ~ Sepal.Length, pch=15) #15だと四角で中にも色が入っています。
plot(Petal.Length ~ Sepal.Length, pch=16) #16だと四角で中にも色が入っています。
plot(Petal.Length ~ Sepal.Length, pch=17) #17だと三角で中にも色が入っています。
点の代わりに文字、数字を入れることもできます。
plot(Petal.Length ~ Sepal.Length, pch="a")

点の色
色はcolで設定します。デフォルトは黒で、1です。
plot(Petal.Length ~ Sepal.Length, col=0) #0だと見えなくなります。
plot(Petal.Length ~ Sepal.Length, col=2) #2だと赤。
plot(Petal.Length ~ Sepal.Length, pch=15, col=2) #四角で中にも色が入っています。
plot(Petal.Length ~ Sepal.Length, pch=15, col="darkgreen") #色を英名で指定することもできます。
どんな色の英名がRにあるのかを調べるならcolorsあるいはcoloursです。
colors()
plot(Petal.Length ~ Sepal.Length, pch=15, col="#ff7f50") #色を16進数で指定することもできます。
16進数での色表現と色の英名、そして色の見本は「原色大辞典」というウェブサイトで調べましょう。
ちなみに上で指定したのは、「原色大辞典」で右上にあるcoralという色です。

複数の形、色の点を描く
同じ図の中に丸と三角の点を描く方法はいろいろあります。
ここでは、白い図を作ってから点を入力する方法を紹介します。
plot(Petal.Length ~ Sepal.Length, type="n")  #type="n"とすると、白い図ができます。
pointsで点を入力します。
points(Petal.Length[Species=="setosa"] ~ Sepal.Length[Species=="setosa"], pch=0, col=2)
points(Petal.Length[Species=="virginica"] ~ Sepal.Length[Species=="virginica"], pch=16, col="darkblue")

pointsの例
pointsのヘルプ画面、?pointsに紹介されていた例を転載します。
require(stats) # for rnorm
plot(-4:4, -4:4, type = "n")  # setting up coord. system
points(rnorm(200), rnorm(200), col = "red")
points(rnorm(100)/2, rnorm(100)/2, col = "blue", cex = 1.5)
#以下では背景色を変えた図にsinカーブを描きます。
op <- par(bg = "light blue")
x <- seq(0, 2*pi, len = 51)
## something "between type='b' and type='o'":
plot(x, sin(x), type = "o", pch = 21, bg = par("bg"), col = "blue", cex = .6,
 main = 'plot(..., type="o", pch=21, bg=par("bg"))')
par(op)

凡例の表示
さきほど複数の形と色の点を描きました。それは以下の通りです。
plot(Petal.Length ~ Sepal.Length, type="n") 
points(Petal.Length[Species=="setosa"] ~ Sepal.Length[Species=="setosa"], pch=0, col=2)
points(Petal.Length[Species=="virginica"] ~ Sepal.Length[Species=="virginica"], pch=16, col="darkblue")
でも、このままだと情報不足で分かりにくいので、凡例を表示します。
凡例はlegendで設定します。
legend(6.5, 2, c("setosa", "virginica"), pch=c(0, 16), col=c(2, "darkblue"))
これでは小さいので、点と文字を大きくします。pt.cexやcexの数値を自由に変えてみてください。
legend(6.5, 2, c("setosa", "virginica"), pch=c(0, 16), col=c(2, "darkblue"), pt.cex=2, cex=1.5)
ちょうどよい数値になったら、図を描き直してから、確定版のlegendを実行しましょう。

回帰直線を引く
これらの点の回帰直線を引いてみます。Petal.Length[Species=="virginica"]がY、
#Sepal.Length[Species=="virginica"]がXなので、interceptが切片です。
plot(Petal.Length ~ Sepal.Length, type="n")
points(Petal.Length[Species=="setosa"] ~ Sepal.Length[Species=="setosa"], pch=0, col=2)
points(Petal.Length[Species=="virginica"] ~ Sepal.Length[Species=="virginica"], pch=16, col="darkblue")
lm(Petal.Length[Species=="setosa"] ~ Sepal.Length[Species=="setosa"])
abline(a=0.8031, b=0.1316, col=2)
lm(Petal.Length[Species=="virginica"] ~ Sepal.Length[Species=="virginica"])
abline(a=0.6105, b=0.7501, col="darkblue", lty=2)  #ltyは線のタイプline typeです。

ablineの他の例
ablineのヘルプ画面、?ablineに紹介されていた例を転載します。
## Setup up coordinate system (with x == y aspect ratio):
plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
## the x- and y-axis, and an integer grid
abline(h = 0, v = 0, col = "gray60")
text(1,0, "abline( h = 0 )", col = "gray60", adj = c(0, -.1))
abline(h = -1:5, v = -2:3, col = "lightgray", lty = 3)
abline(a = 1, b = 2, col = 2)
text(1,3, "abline( 1, 2 )", col = 2, adj = c(-.1, -.1))

마지막 수정됨: 월요일, 15 2월 2021, 6:39 PM