# 高度なグラフィクスを描画することができる関数ggplot() 【ggは The Grammar of Graphicsを表す】 と
# 関数qplot() 【qはquickを表す】について、基本グラフィクス関数のplot()との比較も交えて説明します。
# 関数ggplot()と 関数qplot()は ggplot2パッケージを使います。
# これまでも何度か使ったデータセット mtcars をまずは使って比較します。
# まずは、install.packages("ggplot2")を実行してください。
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.1.3
plot(mtcars$wt, mtcars$mpg) # 基本グラフィクス関数 plot()

qplot(mtcars$wt, mtcars$mpg) # ggplot2パッケージのうちの関数 qplot()

# data=mtcarsを指定することにより、前にある引数は列名の指定だけでOK。この方が記述しやすい。
qplot(wt, mpg, data = mtcars)

# 関数plotや関数qplot では、どんな図を描くが指定しない場合(デフォルト)は散布図を描きます。
# これは、散布図がデータをより忠実に可視化することから、デフォルトを散布図にしています。
# その意味では、ヒストグラムや箱ひげ図は、見方に対して作者の意図が入っていると言えるかもしれません。
# 一方、ggplot()では、まずggplot()により描く対象を何にするかを指定し、その後にどんな図を描画するかを指定します。
# その複数の指定したものは、”+”でつなぎます。 “+”は、重ね合わせるという意味を持ちます。
# geom_point()が散布図を指定しています。
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()

# 次は折れ線グラフです。
# まずは基本関数plot()
plot(pressure$temperature, pressure$pressure, type = "l")
# 点を追加します。
points(pressure$temperature, pressure$pressure)
# 2本目の線と点を追加します。
lines(pressure$temperature, pressure$pressure/2, col="red")
points(pressure$temperature, pressure$pressure/2, col="red")

# 関数qplot()と関数ggplot()を使ってみます。
# 関数qplot()でも、ラインを描く場合には、引数geomにより"line"を指定する必要があります。
qplot(pressure$temperature, pressure$pressure, geom="line")

ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line()

# line と point の両方を描いてみます。
qplot(temperature, pressure, data=pressure, geom=c("line", "point"))

ggplot(pressure, aes(x=temperature, y=pressure)) + geom_line() + geom_point()

# データセットmtcarsを使ってヒストグラムを描きます。
hist(mtcars$mpg, breaks = 10)

qplot(mpg, data = mtcars, binwidth = 2)

ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 2)

# 引数binwidth により1つの棒の幅を指定しています。
# この引数の値を変えて実行してみましょう。
# データセットmtcarsを使って、棒グラフを描きます。
# まずは、シリンダー数4だけの車を選択します。
cyl4 <- subset(mtcars, cyl == 4)
cyl4
## mpg cyl disp hp drat wt qsec vs am gear carb
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# 選んだシリンダー数4の車種について、燃費(mpg)について棒グラフを描き、さらに馬力(hp)により色分けします。
ggplot(cyl4, aes(x = rownames(cyl4), y = mpg, fill = hp)) + geom_bar(stat = "identity")

library(gcookbook)
ups <- subset(uspopchange, rank(Change) > 40)
ups
## State Abb Region Change
## 3 Arizona AZ West 24.6
## 6 Colorado CO West 16.9
## 10 Florida FL South 17.6
## 11 Georgia GA South 18.3
## 13 Idaho ID West 21.1
## 29 Nevada NV West 35.1
## 34 North Carolina NC South 18.5
## 41 South Carolina SC South 15.3
## 44 Texas TX South 20.6
## 45 Utah UT West 23.8
ggplot(ups, aes(x=reorder(Abb, Change), y=Change, fill=Region)) +
geom_bar(stat="identity", colour = "black") +
scale_fill_manual(values=c("green", "black")) +
xlab("State")

# その他1:円グラフについては、Rはきれいな図を描くことができません。
# ggplot2パッケージではなく、基本グラフィクスとして関数pie()が用意されているだけです。
# また、3時の方向から始まることについて違和感を感じます。
pie(c(10, 20, 30), labels=c("OK", "NG", "Not fixed"))

# その他2:ggplot2では地図も用意されています。
# install.packages("maps") をまずは実行してください。
library(maps)
world_map <- map_data("world")
# どんな地図が用意されているか確認します。
sort(unique(world_map$region))
## [1] "Afghanistan" "Albania"
## [3] "Algeria" "American Samoa"
## [5] "Andaman Islands" "Andorra"
## [7] "Angola" "Anguilla"
## [9] "Antarctica" "Antigua"
## [11] "Aral Sea" "Argentina"
## [13] "Aruba" "Australia"
## [15] "Austria" "Azores"
## [17] "Bahamas" "Bahrain"
## [19] "Bangladesh" "Barbados"
## [21] "Barbuda" "Belgium"
## [23] "Belize" "Benin"
## [25] "Bhutan" "Black Sea"
## [27] "Bolivia" "Bonaire"
## [29] "Botswana" "Brazil"
## [31] "Brunei" "Bulgaria"
## [33] "Burkina Faso" "Burundi"
## [35] "California" "Cambodia"
## [37] "Cameroon" "Canada"
## [39] "Canary Islands" "Cape Verde"
## [41] "Caspian Sea" "Cayman Islands"
## [43] "Central African Republic" "Chad"
## [45] "Chagos Archipelago" "Chile"
## [47] "China" "Colombia"
## [49] "Comoros" "Congo"
## [51] "Cook Islands" "Costa Rica"
## [53] "Cuba" "Curacao"
## [55] "Cyprus" "Czechoslovakia"
## [57] "Denmark" "Djibouti"
## [59] "Dominica" "Dominican Republic"
## [61] "Ecuador" "Egypt"
## [63] "El Salvador" "Equatorial Guinea"
## [65] "Ethiopia" "Falkland Islands"
## [67] "Fiji" "Finland"
## [69] "France" "French Guiana"
## [71] "French Polynesia" "Gabon"
## [73] "Gambia" "Gaza Strip"
## [75] "Germany" "Ghana"
## [77] "Great Bitter Lake" "Great Lakes"
## [79] "Greece" "Greenland"
## [81] "Grenada" "Guadeloupe"
## [83] "Guatemala" "Guinea"
## [85] "Guinea-Bissau" "Guyana"
## [87] "Haiti" "Hawaii"
## [89] "Honduras" "Hungary"
## [91] "Iceland" "India"
## [93] "Indonesia" "Iran"
## [95] "Iraq" "Ireland"
## [97] "Irian Jaya" "Isle of Man"
## [99] "Isle of Wight" "Israel"
## [101] "Italy" "Ivory Coast"
## [103] "Jamaica" "Japan"
## [105] "Jordan" "Kenya"
## [107] "Kiribati" "Kuwait"
## [109] "Lacul Greaca" "Lake Albert"
## [111] "Lake Fjerritslev" "Lake Kariba"
## [113] "Lake Malawi" "Lake Pasvikelv"
## [115] "Lake Tanganyika" "Lake Titicaca"
## [117] "Lake Victoria" "Laos"
## [119] "Lebanon" "Lesotho"
## [121] "Liberia" "Libya"
## [123] "Liechtenstein" "Luxembourg"
## [125] "Madagascar" "Madeira Islands"
## [127] "Malawi" "Malaysia"
## [129] "Maldives" "Mali"
## [131] "Malta" "Marshall Islands"
## [133] "Martinique" "Maug Island"
## [135] "Mauritania" "Mauritius"
## [137] "Mexico" "Micronesia"
## [139] "Monaco" "Mongolia"
## [141] "Montserrat" "Morocco"
## [143] "Mozambique" "Myanmar"
## [145] "Namibia" "Nauru"
## [147] "Nepal" "Netherlands"
## [149] "Neutral Zone" "Nevis"
## [151] "New Caledonia" "New Zealand"
## [153] "Nicaragua" "Niger"
## [155] "Nigeria" "North Korea"
## [157] "Northern Mariana Islands" "Norway"
## [159] "Oman" "Pakistan"
## [161] "Panama" "Papua New Guinea"
## [163] "Paracel Islands" "Paraguay"
## [165] "Peru" "Philippines"
## [167] "Pitcairn Islands" "Poland"
## [169] "Portugal" "Puerto Rico"
## [171] "Qatar" "Romania"
## [173] "Rwanda" "Saint Eustatius"
## [175] "Saint Kitts" "Saint Lucia"
## [177] "Saint Vincent" "Saint-Barthelemy"
## [179] "Saint-Martin" "Samoa"
## [181] "San Marino" "Sao Tome and Principe"
## [183] "Sardinia" "Saudi Arabia"
## [185] "Senegal" "Seychelles"
## [187] "Sicily" "Sierra Leone"
## [189] "Sin Cowe Island" "Solomon Islands"
## [191] "Somalia" "Sonsorol Island"
## [193] "South Africa" "South Korea"
## [195] "South Sandwich Islands" "Spain"
## [197] "Spratly Island" "Sri Lanka"
## [199] "Sudan" "Suriname"
## [201] "Swaziland" "Sweden"
## [203] "Switzerland" "Syria"
## [205] "Tanzania" "Thailand"
## [207] "Tobago" "Togo"
## [209] "Tokelau" "Tonga"
## [211] "Trinidad" "Tunisia"
## [213] "Turkey" "Turks and Caicos"
## [215] "Tuvalu" "Uganda"
## [217] "UK" "United Arab Emirates"
## [219] "Uruguay" "USA"
## [221] "USSR" "Vanuatu"
## [223] "Venezuela" "Vietnam"
## [225] "Virgin Islands" "Vislinskiy Zaliv"
## [227] "Wales" "West Bank"
## [229] "Western Sahara" "Yemen"
## [231] "Yugoslavia" "Zaire"
## [233] "Zambia" "Zimbabwe"
# 日本の地図を選びます。
jpn <- map_data("world", region="Japan")
# 日本の地図を描きます。ただし、あまりかっこ良くない。
ggplot(jpn, aes(x=long, y=lat, group=group, fill=region)) +
geom_polygon(colour = "black") +
scale_fill_brewer(palette = "Set2")
