Rで色々な棒グラフを作成してみた

# ggplot2の基礎知識は岩嵜 航 氏のサイトへ
https://heavywatal.github.io/rstats/ggplot2.html

# 解析環境
OS: Windows 10
App: R Version 3.4.3

# 図にしたいデータ
|# |Clusters |x.stage |y.stage |z.stage|
|:-:|:------:|:-------:|:------:|:-----:|
|1|     black|      91|     190|     127|
|2|     green|     163|     564|     258|
|3| turquoise|     318|     420|     149|
|4|       red|     200|     381|     295|

出典：
https://www.biostars.org/p/92218/

# 必要なlibraryは２つだけ
library(ggplot2)
library(reshape2)

# Rにデータを読み込ませるよ
```
dat=read.table(header=T, text="Clusters x-stage y-stage    z-stage
+ black         91         190    127
+ green        163         564    258
+ turquoise    318         420    149
+ red          200         381     295")
```

# データを整形して使いやすくする
```
dm=melt(dat,id.var=c("Clusters"))
```
Clusters variable value
1      black  x.stage    91
2      green  x.stage   163
3  turquoise  x.stage   318
4        red  x.stage   200
5      black  y.stage   190
6      green  y.stage   564
7  turquoise  y.stage   420
8        red  y.stage   381
9      black  z.stage   127
10     green  z.stage   258
11 turquoise  z.stage   149
12       red  z.stage   295

# x軸にvariable、y軸にvalueの棒グラフ（Cluster分は統合）
```
p1 <- ggplot(dm, aes(variable, value, fill=variable)) + 
theme_bw() + geom_histogram(stat="identity")
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/ddf0bbe3-4d10-b1b6-1d47-123275419038.png)

# x軸にvariable、y軸にvalue、Clustersの積み上げ棒グラフ
```
p1 <- ggplot(dm, aes(variable, value, fill=Clusters)) + 
theme_bw() + geom_bar(stat="identity")
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/2fb40f2f-60f3-5516-44e0-0baf8f316590.png)

# x軸にvariable、y軸にvalue、Clustersを横並びにした棒グラフ

```
p1 <- ggplot(dm, aes(variable, value, fill=Clusters)) + theme_bw() + 
geom_bar(stat="identity", position = "dodge")
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/4ac5c440-fbe9-f093-7030-6a78d4ff0d92.png)


# x軸にvariable, y軸にvalueを共通としたClusterごとの棒グラフ
参考
https://www.biostars.org/p/92218/

```
p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() +
  geom_histogram(stat="identity") + facet_grid(. ~ Clusters)
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/f39b52aa-1e0f-3423-4166-1459df37ba6f.png)

# 横にして数値を表記させる
参考
- https://stackoverflow.com/questions/6017460/position-geom-text-on-dodged-barplot
- https://qiita.com/hoxo_m/items/267ce2ab0acc319599ff

```
p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() + 
geom_histogram(stat="identity") + facet_grid(. ~ Clusters) + coord_flip() + 
geom_text(aes(variable, value, label = sprintf("%2.1f", value)),
position = position_dodge(width = 1))
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/c4542d5d-41fc-aa25-639f-38fc877ab2a8.png)

# y軸の並べ方を変更する
参考
https://stackoverflow.com/questions/34227967/reversed-order-after-coord-flip-in-r

```
p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() + 
geom_histogram(stat="identity") + facet_grid(. ~ Clusters) + coord_flip() + 
geom_text(aes(label = sprintf("%2.1f", value)),
position = position_dodge(width = 1)) + scale_x_discrete(limits = rev(levels(dm$variable)))
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/62a38484-ee64-9ee1-6822-b736e70f221e.png)

# labelの位置を揃えて数値を見やすくする
```
p1 <- ggplot(dm, aes(variable, value, fill=variable)) + theme_bw() + 
geom_histogram(stat="identity") + 
facet_grid(. ~ Clusters) + 
coord_flip() + 
geom_text(aes(label = format(value, digits = 4), y = 0.7)) + 
scale_x_discrete(limits = rev(levels(dm$variable)))
```
![image.png](https://qiita-image-store.s3.amazonaws.com/0/199071/5c6bd5e1-7408-6cc6-a0d6-2beb22002284.png)

※ 他のデータでは右端に揃うのにこのデータでは左端にめり込んでしまう