Upload
takayuki-nuimura
View
976
Download
0
Embed Size (px)
DESCRIPTION
Presentation on Nagoya.R#10
Citation preview
はじめに ggplot2 の基本 プロット例 おわりに
ggplot2 in action
縫村崇行Takayuki NUIMURA
Nagoya.R#10 (2013/07/27)
1 / 17
はじめに ggplot2 の基本 プロット例 おわりに
自己紹介
専門:GISや RSによるヒマラヤの氷河の変動把握
所属:名大・環境学・雪氷圏変動研究室
OSGeo財団日本支部運営委員
主な業務内容氷河の空間分布 DB作成
ヒマラヤでフィールド調査 (DGPS)
3Dモニターを使ってステレオ写真測量
研究室で GISや R言語の指導
2 / 17
はじめに ggplot2 の基本 プロット例 おわりに
ggplot2とは
http://ggplot2.org/の説明を訳すと
グラフィックス文法に基づいた記述により · · ·—(グダグダ難しいことを述べているので省略)—
⇓
まあなんやかんやで、複雑な図でも ggplot2を使えば簡単に出来る
http://en.wikipedia.org/wiki/Ggplot2 (Wikipediaにも項目があります)
2005年に Hadley Wickhamにより開発されたパッケージ、現在のバージョンは 0.9.3.1
3 / 17
はじめに ggplot2 の基本 プロット例 おわりに
ggplot2とは
R業界ではきれいな図が作れるという事で有名
=⇒個人的にはデータに対して切り口 (プロットの種類)を変えて可視化を試行錯誤するのが簡単で利用しています
4 / 17
はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2を使わない場合)
使用データ: airquality (Rに最初から入っているサンプルデータ)
Ozone Solar.R Wind Temp Month Day41 190 7.4 67 5 136 118 8.0 72 5 2...
......
......
...
plot(airquality$Wind, airquality$Temp)
5 10 15 20
6070
8090
airquality$Wind
airq
ualit
y$Te
mp
5 / 17
はじめに 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 20Wind
Tem
p
6 / 17
はじめに 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 20Wind
Tem
p
100
200
300
Solar.R
7 / 17
はじめに 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 20Wind
Tem
p
Solar.R
100
200
300
8 / 17
はじめに ggplot2 の基本 プロット例 おわりに
基本的なプロット (ggplot2)
ggplot2でのプロットは、
ggplot関数
使用するデータフレームの指定 (今回は airquality)
aes関数で、X軸や Y軸、その他 (色、サイズなど)の変数を指定
geom_*関数
プロットタイプの指定geom_point: 散布図、geom_hist: ヒストグラム etc.
その他の装飾関数
xlab, ylab: 軸タイトル
xlim, ylim: 軸範囲
scale_*関数: 色の指定など9 / 17
はじめに ggplot2 の基本 プロット例 おわりに
時系列データのプロット
使用データ: CSV形式の気温データ (ダミーデータ) Download
time temp2001-01-01 3.62001-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
はじめに 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 2010time
tem
p
11 / 17
はじめに 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/Jantime
tem
p
12 / 17
はじめに ggplot2 の基本 プロット例 おわりに
空間データのプロット
使用データ: 名大周辺の OpenStreetMapデータ Download
OpenStreetMap: 自由な地図データ
位置情報以外に属性情報 (作成ユーザー、時期 etc.) も
ダウンロード後、一般的に GIS (地理情報システム)で使われる形式 (Shapefile)に変換
13 / 17
はじめに 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
はじめに 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.00long
lat
15 / 17
はじめに 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.00long
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
はじめに ggplot2 の基本 プロット例 おわりに
おわりに ggplot2ユーザーとして個人的な感想
ggplot2パッケージは図の綺麗さだけでなく、多変数の関係性を色んな切り口 (プロット)で見るのに便利
基本的なプロット以外に、時系列、空間プロットも可能だが、前処理が必要
まだ version 1.0未満だからなのか、たまに記述ルールが変わる (´・ω・` )
17 / 17