ggplot2で棒グラフに値を追加する方法メモ

# 棒グラフに数値を入れる

棒グラフの並べ方は`position`で指定できます。
参考：https://qiita.com/swathci/items/b08496d863bca4b479b3#グラフの色分け

- `position = position_stack(), "stack"`：積み上げ棒グラフ
- `position = position_fill(), "fill"`：100％積み上げ縦棒
- `position = position_identity(), "identity"`：棒グラフの重ね合わせ
- `position = position_dodge(), "dodge"`：横並びの棒グラフ
- `position = position_dodge2(), "dodge2"`：横並びの棒グラフ

それぞれの場合について、`geom_text()`で数値を入れてみたいと思います。


## 積み上げ棒グラフ：position = position_stack(), "stack"

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_stack(), alpha = 1/3)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/54713d58-f523-a3af-ce47-ac24830511fe.png)

このグラフに数値を入れます。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack())
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_stack(), alpha = 1/3) +
  stat_count(geom = "text", aes(label = ..count..),
             position = position_stack())

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 1))  # vjust = 1：デフォルト

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 0.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_stack(vjust = 0.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_stack(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 0.5), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ede27f6e-a7ed-09bd-3b95-61c267aa9160.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/10685d82-b8fa-63d5-ef9c-dc25b2ab62db.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8201aad0-86da-7c27-4b8c-1f74958e6a64.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/375b50a5-bf04-720c-8220-d3b98cc6b898.png)

#### reverse = TRUE

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_stack(reverse = TRUE), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(reverse = TRUE, vjust = 0.5), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/c1dca795-eeb9-5d47-2c33-f78dc416770f.png)



## 100％積み上げ縦棒：position_fill(), "fill"

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_fill(), alpha = 1/3)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/3d7beaf4-39b4-b407-6c2b-a596fe3b9b3d.png)

このグラフに数値を入れます。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(vjust = 1))  # vjust = 1：デフォルト

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(vjust = 0.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_fill(vjust = 0.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_fill(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(vjust = 0.5), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/96751555-e5fc-52f7-0390-ec7db40af117.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ec26a7d0-af12-3205-19b9-377c912b6a36.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/995e95ed-9570-2e66-0993-4b923edd6375.png)

#### reverse = TRUE

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_fill(reverse = TRUE), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(reverse = TRUE, vjust = 0.5), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8498d460-2464-e4e3-12a8-27a612e4fe03.png)


## 棒グラフの重ね合わせ：position_identity(), "identity"

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_identity(), alpha = 1/3)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e346925a-01b8-4995-c0b1-e1e5eb695779.png)

このグラフに数値を入れます。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_identity())

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..+1.5),
            position = position_identity())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = 0, y = +1.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..-1.5),
            position = position_identity())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = 0, y = -1.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_nudge(x = 0, y = +1.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_identity(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = 0, y = +1.5), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/5, width = 0.6) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_nudge(x = -0.4, y = +1.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_identity(), alpha = 1/5, width = 0.6) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = -0.4, y = +1.5), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/2703d6df-8b9e-07b1-ba5e-aefbce5d25a8.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/79bfdfa4-e0ed-f30e-bb2f-c0939c5687d1.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/75e02c8f-f344-2de1-3339-3a6d30454ad2.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7e4f6741-b4e6-ef2b-b58f-d97e9cec3e7d.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/fdbc8990-1caa-2ca2-e343-86cdfed193ae.png)


## 横並びの棒グラフ：position_dodge(), "dodge"

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_dodge(), alpha = 1/3)

ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "single"), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_dodge(preserve = "single"), alpha = 1/3)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/6d7b855e-5603-2cf3-8a8a-21c9e8f169bf.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/236d60d1-519c-aa58-2444-cd658348e9a5.png)

このグラフに数値を入れます。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(width = 0.9), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_dodge(width = 0.9))
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_dodge(preserve = "total", width = 0.9))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count..+1.5),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..+1.5),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count../2),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count../2),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/f81e1411-a000-ff7f-9588-b260c3b89be4.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9aea090c-fbf6-3418-ecf3-08edc584fb7d.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/8270dd80-f0f3-db42-9bf9-eac47e4773c9.png)


## 横並びの棒グラフ：position_dodge2(), "dodge2"

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single"), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_dodge2(preserve = "single"), alpha = 1/3)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/24c4f075-85b9-52fe-fc1a-e01a965dec45.png)

このグラフに数値を入れます。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_dodge2(preserve = "single", width = 0.9))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count..+1.5),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..+1.5),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count../2),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count../2),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/cf2c75ed-d7b6-61b5-4c86-d39385e77900.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7d31f5b2-2cf0-777c-3758-1667c188ccc9.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7622f5df-18a5-dcde-b4ab-ee3ab3c99ccc.png)

#### reverse = TRUE

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge2(preserve = "single", width = 0.9, reverse = TRUE),
           alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count../2),
            position = position_dodge2(preserve = "single", width = 0.9, reverse = TRUE),
            show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/a3cc7d49-4a12-6160-c62d-3efe85821daf.png)


## 横並びの棒グラフでグループごとの位置をそろえる

横並びの`position = position_dodge()`, `position = position_dodge2()`では、x軸ごとにグループの数が異なる場合はグループの位置がそろいません。
グループの位置をそろえるには、`position = position_identity()`で直接的に位置を指定すればよさそうです。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar",
             aes(color = Species, fill = Species,
                 x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
             position = position_identity(), width = 0.3, alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  stat_count(geom = "bar",
             aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
             position = position_identity(), width = 0.3, alpha = 1/3)

p <- ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count")
layer_data(p)
#     y count prop x flipped_aes group PANEL ymin ymax xmin xmax colour   fill size linetype alpha
# 1   5     5 0.10 4       FALSE     1     1    0    5 3.55 4.45     NA grey35  0.5        1    NA
# 2  47    40 0.80 5       FALSE     1     1    7   47 4.55 5.45     NA grey35  0.5        1    NA
# 3  68     5 0.10 6       FALSE     1     1   63   68 5.55 6.45     NA grey35  0.5        1    NA
# 4   7     6 0.12 5       FALSE     2     1    1    7 4.55 5.45     NA grey35  0.5        1    NA
# 5  63    36 0.72 6       FALSE     2     1   27   63 5.55 6.45     NA grey35  0.5        1    NA
# 6  24     8 0.16 7       FALSE     2     1   16   24 6.55 7.45     NA grey35  0.5        1    NA
# 7   1     1 0.02 5       FALSE     3     1    0    1 4.55 5.45     NA grey35  0.5        1    NA
# 8  27    27 0.54 6       FALSE     3     1    0   27 5.55 6.45     NA grey35  0.5        1    NA
# 9  16    16 0.32 7       FALSE     3     1    0   16 6.55 7.45     NA grey35  0.5        1    NA
# 10  6     6 0.12 8       FALSE     3     1    0    6 7.55 8.45     NA grey35  0.5        1    NA
layer_data(p)$group
# [1] 1 1 1 2 2 2 3 3 3 3
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_nudge(x = (layer_data(p)$group-2)*0.9/3, y=0),
           width = 0.3, alpha = 1/3)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1aeaa1ea-4fe0-5de6-2e96-ab85f30e07b2.png)

このグラフに数値を入れます。

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/3) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count.., color = Species,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count.., color = Species,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count../2),
            position = position_identity(), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count../2),
            position = position_identity(), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count.., color = Species,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3,
                y = ..count..-..count..+1.5),
            position = position_identity(), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3,
                y = ..count..-..count..+1.5),
            position = position_identity(), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b44692ae-8fca-13b5-a41a-6364ca0d51cd.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5ce0c9eb-8fd0-4d25-0003-559ab9bc9d6b.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/20a43cb1-31c9-bca8-8072-56af5792d89d.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/10238d3d-5f86-6123-964d-f90b9848e0b4.png)

#### reverse

```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (2-as.integer(Species))*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (2-as.integer(Species))*0.9/3, y = ..count../2),
            position = position_identity(), show.legend = FALSE) +
  xlim(4-0.9*3/6, 8+0.9*3/6)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/77fe8e34-6c37-f511-82b8-8d7205fddc67.png)


```R:R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_identity(), alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/ebe9a61f-a61a-c916-b928-260499e3b107.png)


# 参考
- https://qiita.com/swathci/items/b36e493ea78b03db0981
- https://qiita.com/swathci/items/b08496d863bca4b479b3
- https://qiita.com/swathci/items/00b4a847843a33345887