・お題:Rでグラフを描くライブラリとして代表的なものに、ggplot2というのがある。苦手で避けてきた。使いそうなやつを纏めて、しのぎたい。
・ライブラリを読み込む。tidyverseにggplot2が入っているので、tidyverseを読み込む。
> library(tidyverse)
・データセットを作る。
> set1 <- 1:100
> set2 <- rnorm(n = 100,
+ mean = 0,
+ sd = 2)
> set3 <- runif(n = 100,
+ min = -10,
+ max = 10)
> set4 <- set3 %>%
+ as.integer(.)
> set5 <- sample(x = c("Group_A", "Group_B", "Group_C"),
+ size = 100,
+ replace = TRUE,
+ prob = c(2,3,4))
> set5 %>% table(.)
Group_A Group_B Group_C
16 32 52
・データフレームにまとめる。
> df <- data.frame(set1, set2, set3, set4, set5)
> df %>% str(.)
> df %>% head(.)
・ggplot2に入る。いろいろなやり方があるらしいが、今回はとりあえず、データを読み込む。
> p <- ggplot(data = df)
> hist1 <- p +
+ geom_histogram(mapping = aes(x = set2),
+ binwidth = 0.5) +
+ labs(title = "title1", #表題
+ x = "x-jiku") + #X軸の名前
+ theme_classic() #テーマ
> plot(hist1)
・別パターンも描いてみる。
> hist2 <- p+
+ geom_freqpoly(mapping = aes(x = set2,
+ # y = after_stat(density), #比率にする
+ color = as.factor(set5)),
+ binwidth = 1,
+ size = 2,
+ alpha = 0.5)+
+ scale_color_manual(name = "Groups", labels = c("A", "B", "C"), values = c("orange", "blue", "purple"))+ #手動で色を選択
+ labs(title = "hist2")+
+ labs(title = "title1",
+ x = "x-jiku") +
+ theme_classic()
> plot(hist2)
・もう一つ、別パターンを描いてみる。これは密度プロット。
> hist3 <- p +
+ geom_density(mapping = aes(x = set2,
+ color = as.factor(set5)),
+ size = 1,
+ alpha = 0.3)+
+ scale_color_manual(name = "Groups", labels = c("A", "B", "C"), values = c("orange", "blue", "purple"))+ #手動で色を選択
+ labs(title = "hist2")+
+ labs(title = "title1",
+ x = "x-jiku") +
+ theme_classic()
> plot(hist3)
・折れ線グラフ
> line1 <- p +
+ geom_line(mapping = aes(x = set1, y = set2, color = as.factor(set5)), #X軸、Y軸、色の基準
+ size = 1, #プロットの大きさ
+ linetype = "solid", #ラインの形状 #使うとしたら"dashed", "dotted", "dotdash"あたり
+ # color = "blue",
+ alpha = 0.5) + #透明度
+ scale_color_manual(name = "Groups", labels = c("A", "B", "C"), values = c("orange", "blue", "purple"))+ #手動で色を選択
+ xlim(0, 100) + #X軸の表示の範囲
+ ylim(-10, 10) + #Y軸の表示の範囲
+ labs(title="title1",
+ x = "x-jiku",
+ y = "y-jiku")+
+ theme_classic()
> plot(line1)
・散布図
> scatter1 <- p +
+ geom_point(mapping = aes(x = set2, y = set3,
+ color = as.factor(set5),#色の基準
+ # shape = as.factor(set5) #形の基準
+ ),
+ size = 3, #プロットの大きさ
+ # color = "blue", #色
+ alpha = 0.5) + #透明度
+ # scale_color_brewer(name = "Groups", labels = c("A", "B", "C"), palette = "Accent")+ #カラーパレットで選択
+ scale_color_manual(name = "Groups", labels = c("A", "B", "C"), values = c("orange", "blue", "purple"))+ #手動で色を選択
+ # scale_shape_manual(name = "Groups", labels = c("A", "B", "C"), values = c("circle", "triangle", "square"))+ #手動で形を選択
+ xlim(-10, 10) +
+ ylim(-10, 10) +
+ labs(title="title1",
+ x = "x-jiku",
+ y = "y-jiku")+
+ theme_classic()
> plot(scatter1)
・散布図+回帰直線。
> scatter2 <- p +
+ geom_point(mapping = aes(x = set3, y = set4),
+ size = 3, #プロットの大きさ
+ color = "blue", #色
+ alpha = 0.5) +
+ geom_smooth(mapping = aes(x = set3, y = set4),
+ method = lm, #手法。今回は直線
+ se = TRUE, #信頼区間の表示の有無
+ color = "red") +
+ xlim(-10, 10) + #X軸の表示の範囲
+ ylim(-10, 10) + #Y軸の表示の範囲
+ labs(title="title1", #表題
+ x = "x-jiku", #X軸の名前
+ y = "y-jiku")+ #Y軸の表示の名前
+ theme_classic() #テーマ
> plot(scatter2)
`geom_smooth()` using formula = 'y ~ x'
・箱ひげ図
> box1 <- p +
+ geom_boxplot(mapping = aes(x = as.factor(set5), y = set2, fill = as.factor(set5)),
+ color = "gray30", #線の色
+ size = 1, #線の太さ
+ alpha = 0.5) + #箱ひげ図
+ geom_jitter(mapping = aes(x = as.factor(set5), y = set2), #個別値プロット
+ alpha = 0.3,
+ width = 0.1) + #散らかり具合の幅
+ scale_fill_manual(name = "Groups", labels = c("A", "B", "C"), values = c("orange", "blue", "purple")) + #手動で色を選択
+ ylim(-10, 10) +
+ labs(title="title1",
+ x = "",
+ y = "y-jiku")+
+ theme_classic()
> plot(box1)
・円グラフ。。。はgeom_pieとかがあるのかと思ったが、なかった。棒グラフを丸める感じらしい。正直、描きづらい。。
> pie1 <- p+
+ geom_bar(mapping = aes(x = "x", fill = as.factor(set5)),
+ stat = "count",
+ position = position_stack(reverse = TRUE)) +
+ scale_fill_manual(name = "Groups", labels = c("A", "B", "C"), values = c("orange", "blue", "purple")) + #手動で色を選択
+ coord_polar(theta = "y")
> plot(pie1)
・グラフはここまでで終わり。名前の付いた色の参考は以下。
・と、ここまで書いて気づいたが、一番使いそうな(?)棒グラフをやっていなかった。そのうちやりたい。。
いったんおわり。