#author("2019-02-27T10:06:12+00:00","","") [[R言語入門]] > * Rでグラフ描画 [#t7fcddf2] #setlinebreak(on); #contents -- 関連 --- [[R言語入門]] --- [[機械学習の為の数学の基礎]] ** グラフ描画の基本 [#yced50a2] #html(<div style="padding-left: 10px;">) グラフ描画は基本的に以下の手順で行う。 + 描画デバイスを開く + グラフデータをプロットする + グラフの装飾(追加情報の描画) + 描画デバイスを閉じる ※ R Studio の場合は描画デバイスのオープン、クローズは無くてもOK。(プロット領域に表示される) 折れ線グラフの描画例) #myterm2(){{ x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 4, 8, 10) dev.new() # 1. 描画デバイスを開く plot(x, y) # 2. データのプロット lines(x, y) # 3. 線の追加 ※ plot(x, y, type="b") 等で一発で書く事も可能 dev.off() # 4. 描画デバイスを閉じる }} #html(</div>) ** plot 関数の使用方法 [#naf31e6f] #html(<div style="padding-left: 10px;">) グラフ描画で最もよく使用する plot について記載する。 形式) | plot(x, y, ...) | 引数) | 引数 | 説明 |h | x | X軸のデータ | | y | Y軸のデータ | | type | "p": 点を描画 | |~| "l": 線のみを描画 | |~| "b": 点と線の両方を描画 | |~| "c": 線のみを描画( 点の部分は空白になる ) | |~| "o": 点と線の両方を描画( "l" との違いは点の描画域の余白がない事 ) | |~| "h": 垂直線の描画(ヒストグラム?) | |~| "s": 階段グラフの描画 | |~| "S" s の反転グラフ | |~| "n": データの描画はしない(プロット領域の初期化のみ行う) | | main | タイトル(描画領域の上部に表示される) | | sub | サブタイトル(描画領域の下部に表示される) | | xlab | X軸のタイトル | | ylab | Y軸のタイトル | | asp | x/y のアスペクト比。&br;※ plot.windowで使用されるパラメータ | | xlim | X軸の描画範囲&br;※ plot.windowで使用されるパラメータ | | ylim | Y軸の描画範囲&br;※ plot.windowで使用されるパラメータ | | log | 対数スケールする軸を指定する ※ "x"or "y" or "xy" | | ann | タイトル、X軸 及び Y軸のラベルを表示するかどうか。(T or F)| | axes | X軸、Y軸を表示するかどうか。( T or F )&br;※ Fにした場合は枠自体が表示されない。 | | xaxt&br;yaxt | X軸、Y軸メモリを表示するかどうか。( 表示しない場合は "n" を指定 ) | | frame.plot | 枠を表示するかどうか ( T or F ) | | panel.first | プロット軸が設定された後、プロットが行われる前に評価される式。| | panel.last | プロットが行われた後、Axes、title、boxが追加される前に評価される式。 | // cex 点のサイズ、指定しない場合は1 // cex // A numerical value giving the amount by which plotting text and symbols should be magnified relative to the default. This starts as 1 when a device is opened, and is reset when the layout is changed, e.g. by setting mfrow. // Note that some graphics functions such as plot.default have an argument of this name which multiplies this graphical parameter, and some functions such as points and text accept a vector of values which are recycled. // cex.axis // The magnification to be used for axis annotation relative to the current setting of cex. // cex.lab // The magnification to be used for x and y labels relative to the current setting of cex. // cex.main // The magnification to be used for main titles relative to the current setting of cex. // cex.sub // The magnification to be used for sub-titles relative to the current setting of cex. 例えば、以下のように type="l" を指定すれば線だけを描画できる。 #myterm2(){{ x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 4, 8, 10) dev.new() plot(x, y, type="l") # 線だけを描画 dev.off() }} #html(</div>) // ** グラフの種類などの設定 [#e2a6e2ec] // http://cse.naro.affrc.go.jp/takezawa/r-tips/r/51.html ** グラフの描画/装飾方法 [#m5fdeef0] #html(<div style="padding-left: 10px;">) *** タイトルの描画 [#s7a9b8b0] #html(<div style="padding-left: 10px;">) #TODO #html(</div>) *** 文字の描画 [#s2165d86] #html(<div style="padding-left: 10px;">) #TODO #html(</div>) *** 座標軸の描画 [#c46723f5] #html(<div style="padding-left: 10px;">) #TODO #html(</div>) *** 凡例の描画 [#ofa4b9d3] #html(<div style="padding-left: 10px;">) #TODO // legend #html(</div>) *** 点の描画 [#b83a4b3c] #html(<div style="padding-left: 10px;">) #TODO // plot(x, y) #html(</div>) *** 追加の点の描画 [#se1d1b61] #html(<div style="padding-left: 10px;">) #TODO // points #html(</div>) *** グリッドの描画 [#h41a04f0] #html(<div style="padding-left: 10px;">) #TODO // grid #html(</div>) *** 線の描画 [#bef6ffa1] #html(<div style="padding-left: 10px;">) #TODO // lines // abline #html(</div>) *** 四角形の描画 [#w6ae4624] #html(<div style="padding-left: 10px;">) #TODO // rect #html(</div>) *** 多角形の描画 [#a327a6c7] #html(<div style="padding-left: 10px;">) #TODO // polygon #html(</div>) *** 矢印の描画 [#g2f738a1] #html(<div style="padding-left: 10px;">) #TODO // arrows #html(</div>) #html(</div>) ** グラフの描画例 [#m411ecaa] #html(<div style="padding-left: 10px;">) *** 点グラフ(線形補間) [#o60d4644] #html(<div style="padding-left: 10px;">) #myterm2(){{ x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 4, 8, 10) dev.new() plot(x, y) points(approx(x, y)) # 線形補間 dev.off() }} #html(</div>) *** 折れ線グラフ [#t92bf867] #html(<div style="padding-left: 10px;">) #myterm2(){{ x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 4, 8, 10) dev.new() plot(x, y) lines(x, y) # 線グラフ描画 dev.off() }} #html(</div>) *** 線形回帰(回帰直線) [#fe092193] #html(<div style="padding-left: 10px;">) #myterm2(){{ x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 4, 8, 10) dev.new() plot(x, y) result <- lm(y~x) # 線型モデルによる回帰分析を行い回帰式を得る fitted_y = fitted(result) # 回帰式により予測値を求める lines(x, fitted_y) # 線グラフの描画 # abline(result) # abline でも同じく回帰直線が描ける(直線回帰の結果を引数に指定する) dev.off() }} #html(</div>) *** ロジスティック回帰 [#o658737b] #html(<div style="padding-left: 10px;">) #TODO #html(</div>) // *** 分散分析 // #html(<div style="padding-left: 10px;">) // #TODO // #html(</div>) *** XXXXXXXXXX [#u305d565] #html(<div style="padding-left: 10px;">) #TODO #html(</div>) *** XXXXXXXXXX [#a28de7ec] #html(<div style="padding-left: 10px;">) #TODO #html(</div>) #html(</div>) ** ファイルへの出力 [#f10227f7] #html(<div style="padding-left: 10px;">) 描画デバイスのOPENを dev.new() でなく、以下の関数で行う事でグラフをファイルに出力する事ができる。 | 関数名 | 説明 | 使用例 | | pdf | PDFファイルとして出力 | | | png | PNG画像として出力 | png("plot1.png", width=480, height=480) | | jpeg | JPEG画像として出力 | | | bmp | BMP画像として出力 | | | postscript | PS画像として出力 | | | pictex | TeXファイルとして出力 | | #html(</div>)