データセットはmtcarsを利用する
参考:“Rグラフィックス クックブック” オライリー・ジャパン
#最も基本のplot関数
plot(mtcars$wt, mtcars$mpg)
# mtcars の列名を指定しないと、列の全ての組合せを1つの図として描画する
# 左上から右下への対角線上に列名が表示されている。これより上と下は、同じものであるが、x軸とy軸が入れ替わっている。
plot(mtcars)
#次にgglpt2パッケージを使う
library(ggplot2)
#まずはqplot関数を使ってみる
qplot(mtcars$wt, mtcars$mpg)
#wtとmpgが同じmtcarsにあるので、次の方法も可能
qplot(wt, mpg, data=mtcars)
#次にggplot関数を使っみる
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
データセット “iris” に対し、qplot関数あるいはggplot関数を使って下に表示した2つの図を描画しなさい。
★ rmd(R mark down)によるhtmlファイルを出力し、Moodleに提出すること。
★ 作成のポイントは、「複数のグラフを1枚の図にまとめる」ことです。
★その方法は、googleを使って調べること。
irisはアイリス(あやめ)です。このデータセットの内容をまず確認すること。
head(iris, 10)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
?iris
##一つ目の作図
##次の通り、4つの別々の図を1つの図にまとめて描画する。
# 参考サイト
# http://id.fnshr.info/2016/10/10/gridextra/
library(gridExtra)
a <- qplot(iris$Species, iris$Petal.Width)
b <- qplot(iris$Species, iris$Petal.Length)
c <- qplot(iris$Species, iris$Sepal.Width)
d <- qplot(iris$Species, iris$Sepal.Length)
grid.arrange(a, b, c, d, ncol=2)
##二つ目の作図
layout1 <- rbind(c(1, 2, 3), c(4, 4, 4))
##二つ目の作図
##個々の図は前の図と同じである。しかし、下の通り、レイアウトが異なっている1枚の図を描画しなさい。
##layout1を定義する必要あり
grid.arrange(a, b, c, d, layout_matrix = layout1)