Mais conteúdo relacionado
Semelhante a 130727 nagoyar presentation (20)
Mais de Takayuki Nuimura (20)
130727 nagoyar presentation
- 1. はじめに ggplot2 の基本 プロット例 おわりに
ggplot2 in action
縫村崇行
Takayuki NUIMURA
Nagoya.R#10 (2013/07/27)
1 / 17
- 2. はじめに ggplot2 の基本 プロット例 おわりに
自己紹介
専門:GIS や RS によるヒマラヤの氷河の変動把握
所属:名大・環境学・雪氷圏変動研究室
OSGeo 財団 日本支部 運営委員
主な業務内容
氷河の空間分布 DB 作成
ヒマラヤでフィールド調査 (DGPS)
3D モニターを使ってステレオ写真測量
研究室で GIS や R 言語の指導
2 / 17
- 3. はじめに ggplot2 の基本 プロット例 おわりに
ggplot2 とは
http://ggplot2.org/ の説明を訳すと
グラフィックス文法に基づいた記述により · · ·
—(グダグダ難しいことを述べているので省略)—
⇓
まあなんやかんやで、複雑な図でも ggplot2 を使えば簡単に出来る
http://en.wikipedia.org/wiki/Ggplot2 (Wikipedia にも項目があります)
2005 年に Hadley Wickham により開発されたパッケージ、
現在のバージョンは 0.9.3.1
3 / 17
- 4. はじめに ggplot2 の基本 プロット例 おわりに
ggplot2 とは
R 業界ではきれいな図が作れるという事で有名
=⇒ 個人的にはデータに対して切り口 (プロットの種類) を変えて
可視化を試行錯誤するのが簡単で利用しています
4 / 17
- 5. はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2 を使わない場合)
使用データ: airquality (R に最初から入っているサンプルデータ)
Ozone Solar.R Wind Temp Month Day
41 190 7.4 67 5 1
36 118 8.0 72 5 2
...
...
...
...
...
...
plot(airquality$Wind, airquality$Temp)
5 10 15 20
60708090
airquality$Wind
airquality$Temp
5 / 17
- 6. はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2 で散布図)
library(ggplot2)#パッケージ読み込み (最初だけ)
#やり方その 1 (シンプルなプロットはこちら)
ggplot(airquality, aes(x=Wind, y=Temp)) + geom_point()
#やり方その 2 (1 行が長くなりそうなときはこちら)
g <- ggplot(airquality, aes(x=Wind, y=Temp))
g <- g + geom_point()
print(g) #g と打つだけでも OK
60
70
80
90
5 10 15 20
Wind
Temp
6 / 17
- 7. はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2 で散布図 + 色)
g <- ggplot(airquality, aes(x=Wind, y=Temp, colour=Solar.R))
g <- g + geom_point()
print(g)
60
70
80
90
5 10 15 20
Wind
Temp
100
200
300
Solar.R
7 / 17
- 8. はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2 で散布図 + サイズ)
g <- ggplot(airquality, aes(x=Wind, y=Temp, size=Solar.R))
g <- g + geom_point()
print(g)
60
70
80
90
5 10 15 20
Wind
Temp
Solar.R
100
200
300
8 / 17
- 9. はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2)
ggplot2 でのプロットは、
ggplot 関数
使用するデータフレームの指定 (今回は airquality)
aes 関数で、X 軸や Y 軸、その他 (色、サイズなど) の変数を
指定
geom_* 関数
プロットタイプの指定
geom_point: 散布図、geom_hist: ヒストグラム etc.
その他の装飾関数
xlab, ylab: 軸タイトル
xlim, ylim: 軸範囲
scale_* 関数: 色の指定など
9 / 17
- 10. はじめに ggplot2 の基本 プロット例 おわりに
時系列データのプロット
使用データ: CSV 形式の気温データ (ダミーデータ) Download
time temp
2001-01-01 3.6
2001-02-01 5.5
...
...
# 時間の列を POSIXct 形式に変換
d <- read.csv("nagoya_temp.csv")
dd <- data.frame(time=strptime(d[,1], format="%Y-%m-%d"), temp=d[,2])
10 / 17
- 11. はじめに ggplot2 の基本 プロット例 おわりに
時系列データのプロット
# POSIXct 形式だと自動で時間目盛を設定してくれる
g <- ggplot(dd, aes(x=time, y=temp))
g <- g + geom_line() + geom_point()
print(g)
10
20
30
2002 2004 2006 2008 2010
time
temp
11 / 17
- 12. はじめに ggplot2 の基本 プロット例 おわりに
時系列データのプロット, 時間目盛を自分で調整
library(scales)# こちらのパッケージも必要
g <- ggplot(dd, aes(x=time, y=temp)) + geom_line() + geom_point()
g <- g + scale_x_datetime(breaks=date_breaks("6 months"),
labels=date_format("%y/%b"))#長いけど 1 行で(;^ω^)
print(g)
10
20
30
01/Jan 01/Jul 02/Jan 02/Jul 03/Jan 03/Jul 04/Jan 04/Jul 05/Jan 05/Jul 06/Jan 06/Jul 07/Jan 07/Jul 08/Jan 08/Jul 09/Jan 09/Jul 10/Jan 10/Jul 11/Jan
temp
12 / 17
- 13. はじめに ggplot2 の基本 プロット例 おわりに
空間データのプロット
使用データ: 名大周辺の OpenStreetMap データ Download
OpenStreetMap: 自由な地図データ
位置情報以外に属性情報 (作成ユーザー、時期 etc.) も
ダウンロード後、一般的に GIS (地理情報システム) で使われ
る形式 (Shapefile) に変換
13 / 17
- 14. はじめに ggplot2 の基本 プロット例 おわりに
空間データのプロット
library(rgdal)# こちらのパッケージが必要
# 前準備としてデータタイプの変換 (sp =⇒ dataframe)
d <- readOGR(“nagoya_lines.shp”, “nagoya_lines”)
d@data$id <- rownames(d@data)
d.points <- fortify(d, region=“id”)
d.df <- join(d.points, d@data, by=“id”)
d.df の中身
long lat order piece group id timestamp user
136.9956 35.15669 1 1 0.1 0 [日時] [ユーザー名]
136.9968 35.15643 2 1 0.1 0 [日時] [ユーザー名]
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
ggplot で空間データをプロットするには、long と lat と group の列
が重要
14 / 17
- 15. はじめに ggplot2 の基本 プロット例 おわりに
空間データのプロット
g <- ggplot(d.df, aes(x=long, y=lat, group=group))
g <- g + geom_path()
print(g)
35.10
35.15
35.20
35.25
136.92 136.96 137.00
long
lat
15 / 17
- 16. はじめに ggplot2 の基本 プロット例 おわりに
空間データのプロット + 属性情報 (ユーザー) で色分け
g <- ggplot(d.df, aes(x=long, y=lat, group=group, colour=user))
g <- g + geom_path()
print(g)
35.10
35.15
35.20
35.25
136.92 136.96 137.00
long
lat
user
42429
anonyex
BugBuster
burai_ha
chikapoko
chosan
dey
ETSUJI_A
grampuskun8
hea−kun
ikiya
Kindmania
Leonidas−from−XIV
makjapan Gcjp
Masaru Okaya
moritoru
M−STA
nal
nishioka
nobichan
nuimura
OSMF Redaction Account
sngmr (Shingo!!!)
takanobu_otsuka
TKE−waka
Tom_G3X
toranosuke
Wingspan
xybot
yohama
16 / 17
- 17. はじめに ggplot2 の基本 プロット例 おわりに
おわりに ggplot2 ユーザーとして個人的な感想
ggplot2 パッケージは図の綺麗さだけでなく、多変数の関係
性を色んな切り口 (プロット) で見るのに便利
基本的なプロット以外に、時系列、空間プロットも可能だ
が、前処理が必要
まだ version 1.0 未満だからなのか、たまに記述ルールが変わ
る (´ ・ω・`)
17 / 17