金子邦彦研究室プログラミングR システムを用いたグラフ描画R システムの plot を用いた種々の散布図と折れ線グラフ

R システムの plot を用いた種々の散布図と折れ線グラフ

要約】 Rシステムのplotを用いて散布図と折れ線グラフを描画する方法について.Rシステムをインストールし,plot関数を使用することで,散布図や折れ線グラフを作成できる.plot関数のオプションには,点や折れ線のスタイル,タイトルの追加,範囲設定,対数目盛り,グラフィックパラメータの調整などがある.また,CRANからのインストールや画像ファイルへの保存も可能である.さらに,ggplot2パッケージを使用することで,散布図や折れ線グラフ,ヒストグラムのバリエーションも実現できる.要点は,Rシステムのplot関数を利用して散布図や折れ線グラフを描画することができることであり,点や折れ線のスタイルやタイトルの追加,範囲設定などの調整が可能であり,ggplot2を活用することでさらなるグラフのバリエーションも得られる.

関連する外部ページ

R システムの CRAN の URL: https://cran.r-project.org/

サイト内の関連ページ

前準備

R システムのインストール

R システムの CRAN の URL: https://cran.r-project.org/

plot の要点

plot() 関数には次のような機能がある

主なオプション

plot() 関数の実行例

まずは,plot() 関数を使い,x, y 値を指定しての散布図の作成を行う.

x 値を格納したベクトル,y 値を格納したベクトルを引数として plot() 関数を使うと,散布図が描かれます.

plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5) )

[image]

[image]

種々の散布図

  1. 折れ線

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="l" );
    

    [image]

    [image]
  2. 点と折れ線(点と折れ線が重ならない)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="b" );
    

    [image]

    [image]
  3. 折れ線(「点」があると思って,折れ線が「点」と重ならないように)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="c" );
    

    [image]
  4. 点と折れ線の重ね合わせ

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o" );
    

    [image]

    [image]

タイトル付け

  1. メインタイトルとサブタイトル

    mainsub を使用. メインタイトルはグラフの上に,サブタイトルはグラフの下に書かれる.

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), main="散布図タイトル", sub="散布図サブタイトル" )
    

    [image]

    [image]
  2. 凡例

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5) )
    legend(5, legend=c("x", "y"))
    

    [image]
  3. x 軸のタイトルとy 軸のタイトル

    xlabylab を使用.

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), xlab="x軸のタイトル", ylab="y軸のタイトル" )
    

    [image]

    [image]
  4. タイトルとして数式を表示

    expression オブジェクトを使う.

    X <- seq(1, 5, length=100)
    Y <- exp(-.5 * log( X^2 ) )
    plot(X, Y, type="o", xlab="x", ylab=expression(e^{-frac(1,2) * {log[10](x)}^2}) )
    

    [image]

    [image]

x,y値の範囲,対数目盛り,xy比

  1. x,y値の範囲

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), xlim=c(0,10), ylim=c(0,6) )
    

    [image]

    [image]
  2. xy比

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), asp="0.4" )
    

    [image]

    [image]
  3. x軸を対数目盛り

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), log="x" )
    

    [image]

    [image]
  4. y軸を対数目盛り

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), log="y" )
    

    [image]

    [image]
  5. x, y軸を対数目盛り

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), log="xy" )
    

    [image]

    [image]
  6. 補助線 (格子状)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), panel.first=grid(8,8) )
    

    [image]

    [image]
  7. 平滑化 (なめらかな折れ線)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5),
            panel.first = lines(stats::lowess( c(8, 7, 6, 5, 4, 3, 2, 1), c(3, 3, 4, 4, 2, 3, 4, 5) ), lty="dashed") )
    

    [image]

    [image]

グラフィックパラメータ

  1. 点や線の色

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", col="red" )
    

    [image]

    [image]
  2. 点の四角形に変更

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", pch = 0 )
    

    [image]

    [image]
  3. 条件により点の形を変える

    X = c(1, 2, 3, 4, 5, 6, 7, 8)
    Y = c(3, 3, 4, 4, 2, 3, 4, 5)
    plot(X,Y,pch = ifelse(Y<4, 0, 1)) 
    

    [image]
  4. 点の大きさ

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", cex=4 )
    

    [image]

    [image]
  5. 線のスタイル(破線)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", lty=2 )
    

    [image]

    [image]
  6. 線のスタイル(点線)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", lty=3 )
    

    [image]

    [image]
  7. 線のスタイル(破線)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", lty=4 )
    

    [image]

    [image]
  8. 線のスタイル(破線)

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", lty=5 )
    

    [image]

    [image]
  9. 線の太さ

    plot( c(1, 2, 3, 4, 5, 6, 7, 8), c(3, 3, 4, 4, 2, 3, 4, 5), type="o", lwd=4 )
    

    [image]