Mukku John Blog

取り組んでいること を つらつら と

ggplot2パッケージを使って、色んなグラフを作成してみる-1

R グラフィックス クックブック 2回目

baseパッケージggplot2パッケージを比べつつ、色んなグラフを作成してみます。
それぞれのグラフに特化した内容は、追って取り扱います。

この記事では、パッケージの違いを目で見て確認するまでにしておきます。
(グラフ作成のコマンド打って、描画されるのを見て、へぇ~と思うまで。)

この記事で対象とするグラフ

  • 散布図
  • 折れ線グラフ
  • 棒グラフ

使うデータ

それぞれのグラフに、利用するデータは、こちらを使います。
R: The R Datasets Package


package:datasetsに含まれるデータセットです。

> library(pryr)
> parenvs(all=TRUE)
   label                                name                   
1  <environment: R_GlobalEnv>           ""                     
2  <environment: package:pryr>          "package:pryr"         
3  <environment: package:ggplot2>       "package:ggplot2"      
4  <environment: 0x000000000c9d8768>    "tools:rstudio"        
・
・
・    
・
15 <environment: package:datasets>      "package:datasets"     
16 <environment: package:methods>       "package:methods"      
17 <environment: 0x000000000bb28d18>    "Autoloads"            
18 <environment: base>                  ""                     
19 <environment: R_EmptyEnv>            ""

散布図

mtcarsデータセットR: Motor Trend Car Road Testsを利用します。

> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

それぞれの軸にプロットする属性は、こちらになります。
横軸:wt Weight (1000 lbs)
縦軸:mpg Miles/(US) gallon

baseパッケージ
> plot(mtcars$wt,mtcars$mpg)

f:id:MukkuJohn:20160809210337p:plain

ggplot2パッケージ
> qplot(mtcars$wt,mtcars$mpg)

f:id:MukkuJohn:20160809210538p:plain
違いは、このへん?

  • 背景
  • 目盛り
  • 点の色
  • 縦軸の数値が縦書き(縦軸が何を指しているかは横書き。残念)

ggplot2パッケージでは、それぞれの軸にマッピングするデータが
同じデータフレームオブジェクトに含まれる場合、下記のコマンドでもOKです。

qplot(wt,mpg,data=mtcars)
#上のコマンドと下のコマンドは同じ
ggplot(mtcars,aes(x=wt,y=mpg)) + geom_point()

折れ線グラフ

pressureデータセットR: Vapor Pressure of Mercury as a Function of Temperatureを利用します。

> head(pressure)
  temperature pressure
1           0   0.0002
2          20   0.0012
3          40   0.0060
4          60   0.0300
5          80   0.0900
6         100   0.2700

それぞれの軸にプロットする属性は、こちらになります。
横軸:temperature (deg C)
縦軸:pressure (mm)

baseパッケージ

線を逐一足していくイメージです。メンドクサ。

> plot(pressure$temperature,pressure$pressure,type = "l")

f:id:MukkuJohn:20160809212134p:plain

#折れ線グラフに点を追加する
points(pressure$temperature,pressure$pressure)

f:id:MukkuJohn:20160809212142p:plain

> #違う線を追加して、点も追加する
> lines(pressure$temperature,pressure$pressure/2, col="red")
> points(pressure$temperature,pressure$pressure/2, col="red")

f:id:MukkuJohn:20160809212241p:plain

線も点も都度足していかないといけないのは、本当にメンドクサイ印象です。

ggplot2パッケージ

1行のコマンドで描画できるけど、幾何オブジェクトに線も点も指定しているという。
折れ線はggplot2パッケージを使ってもメンドクサイ。

qplot(pressure$temperature,pressure$pressure,geom = c("line","point"))

f:id:MukkuJohn:20160809212440p:plain

違いは、このへん?

  • 背景
  • 目盛り
  • 点の色
  • 縦軸の数値が縦書き(縦軸が何を指しているかは横書き。残念)

ggplot2パッケージでは、それぞれの軸にマッピングするデータが
同じデータフレームオブジェクトに含まれる場合、下記のコマンドでもOKです。

qplot(temperature,pressure,data=pressure,geom = "line")
#上のコマンドと下のコマンドは同じ
ggplot(pressure,aes(x=temperature,y=pressure)) + geom_line()
qplot(temperature,pressure,data=pressure,geom = c("line","point"))
#上のコマンドと下のコマンドは同じ
ggplot(pressure,aes(x=temperature,y=pressure)) + geom_line() + geom_point()

棒グラフ

BODデータセットR: Biochemical Oxygen Demandを利用します。

> head(BOD)
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

それぞれの軸にプロットする属性は、こちらになります。
縦軸:A numeric vector giving the time of the measurement (days)
横軸:A numeric vector giving the biochemical oxygen demand (mg/l)

baseパッケージ
> barplot(BOD$demand,names.arg = BOD$Time)

f:id:MukkuJohn:20160809213543p:plain

ggplot2パッケージ
> qplot(BOD$Time,BOD$Time,geom="bar", stat = "identity")

f:id:MukkuJohn:20160809213626p:plain

違いは、このへん?

  • 背景
  • 目盛り
  • 点の色
  • 縦軸の数値が縦書き(縦軸が何を指しているかは横書き。残念)
  • 横軸=6に該当する列の描画有無

データを見ると、確かにTime=6の行はないです。
こんな感じかなと思われます。

  • baseパッケージ:離散値として扱う
  • ggplot2パッケージ:連続値として扱う

ggplot2パッケージに離散値として描画してもらうために、このコマンドを利用します。

qplot(factor(BOD$Time),BOD$Time,geom = "bar", stat = "identity")

factorに変換しています。factorについては、こちら。
mukkujohn.hatenablog.com

ggplot2パッケージでは、それぞれの軸にマッピングするデータが
同じデータフレームオブジェクトに含まれる場合、下記のコマンドでもOKです。

qplot(Time,demand,data=BOD,geom="bar",stat="identity")
#上のコマンドと下のコマンドは同じ
ggplot(BOD,aes(x=Time,y=demand)) + geom_bar(stat = "identity")


次回は、同じテーマでこのグラフを扱います。