金子邦彦研究室プログラミングR システムを用いたグラフ描画散布図,折れ線グラフ,ヒストグラムのバリエーション(R システム,ggplot2 を使用)

散布図,折れ線グラフ,ヒストグラムのバリエーション(R システム,ggplot2 を使用)

このページでは,R システムでのグラフ作成について, 次のことを説明する.
  1. 前準備
  2. ggplot の散布図
  3. ggplot の折れ線グラフ
  4. ggplot の経路のプロット
  5. ggplot のヒストグラム

関連する外部ページ

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

サイト内の関連ページ

前準備

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

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

ggplot2 のインストール

R システムで,次のコマンドを実行し,ggplot2 をインストールする.

このとき「Secure CRAN mirrors」のような,ミラーサイトの選択画面が出たときは「Japan」のものを選ぶ.

install.packages("ggplot2")

ggplot の散布図

geom_point の Aesthetics : x, y, alpha, colour, fill, shape, size

ggplot の折れ線グラフ

geom_line の Aesthetics : x, y, alpha, colour, linetype, size

ggplot の経路のプロット

geom_path の Aesthetics : x, y, alpha, colour, linetype, size

ヒストグラム

ヒストグラム

library(ggplot2)
p <- ggplot(diamonds, aes(x = carat, y = ..density..)) + geom_histogram()
p

[image]

色分け

library(ggplot2)
p <- ggplot(diamonds, aes(x = carat, y = ..density..)) + geom_histogram( aes(colour = ..density.., fill = ..density..))
p

[image]

帯の幅指定

library(ggplot2)
p <- ggplot(diamonds, aes(x = carat, y = ..density..)) + geom_histogram( aes(colour = ..density.., fill = ..density..), binwidth=0.4 )
p

[image]

密度推定等高線

library(ggplot2)
p <- ggplot(diamonds, aes(x = carat, y = depth, z = price)) + geom_contour()
p

[image]
library(ggplot2)
p <- ggplot(data.frame(table(diamonds$color)), aes(x = "", y = Freq, fill = Var1)) 
p + geom_bar( width = 1, stat = "identity" )

[image]

円グラフ

library(ggplot2)
p <- ggplot(data.frame(table(diamonds$color)), aes(x = "", y = Freq, fill = Var1)) 
p + geom_bar( width = 1, stat = "identity" ) + coord_polar("y") 

[image]