ggplot2で円グラフを描く方法メモ

# 円グラフ

ggplot2では、積み上げ棒グラフを極座標に変換することで円グラフが描けます。

この積み上げ棒グラフをもとにします。
（積み上げ棒グラフについては[こちら](https://qiita.com/swathci/items/68d0b0b83cbb4dcad7b8)を参照。）

```R:R
ggplot(data = iris, aes(x = 1, fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count", color = "black")
ggplot(data = iris, aes(x = 1, fill = factor(round(Sepal.Length)))) +
  stat_count(geom = "bar", color = "black")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/7c9dc5b9-37b2-471f-6978-4b984cea88fc.png)

`coord_polar(theta = "y")`で極座標に変換すると円グラフになります。

```R:R
ggplot(data = iris, aes(x = 1, fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count", color = "black") +
  coord_polar(theta = "y")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/53d32676-26e5-4da4-b841-a40195b9636d.png)

積み上げ棒グラフに値を追加します。
（積み上げ棒グラフに値を追加する方法については[こちら](https://qiita.com/swathci/items/68d0b0b83cbb4dcad7b8)を参照。）

```R:R
ggplot(data = iris, aes(x = 1, fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           position = position_stack(), color = "black") +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 0.5)) +
  theme(legend.position = "none")

ggplot(data = iris, aes(x = 1, fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           position = position_stack(), color = "black") +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 0.5)) +
  theme(legend.position = "none") +
  coord_polar(theta = "y")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/25c28102-06a2-c562-bc22-4053acf20d2c.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5d7065c2-2979-2050-b57c-36bc77791b17.png)

凡例を非表示にするかわりにグラフに表示します。

```R:R
ggplot(data = iris, aes(fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(), color = "black") +
  geom_text(stat = "count",
            aes(x = 1, y = ..count..,
                label = ..count..),
            position = position_stack(vjust = 0.5)) +
  geom_text(stat = "count",
            aes(x = 1.55, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5)) +
  theme(legend.position = "none")

ggplot(data = iris, aes(fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(), color = "black") +
  geom_text(stat = "count",
            aes(x = 1, y = ..count..,
                label = ..count..),
            position = position_stack(vjust = 0.5)) +
  geom_text(stat = "count",
            aes(x = 1.55, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5)) +
  theme(legend.position = "none") +
  coord_polar(theta = "y")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/e90487ee-917d-38e3-4a22-e7b77103c1c4.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/5fade2f9-91c5-32a3-97a4-4e51ee684bae.png)

積み上げの順を逆にして、円グラフの順を反時計回りにします。

```R:R
ggplot(data = iris, aes(fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(reverse = TRUE), color = "black") +
  geom_text(stat = "count",
            aes(x = 1, y = ..count..,
                label = ..count..),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  geom_text(stat = "count",
            aes(x = 1.55, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  theme(legend.position = "none")

ggplot(data = iris, aes(fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(reverse = TRUE), color = "black") +
  geom_text(stat = "count",
            aes(x = 1, y = ..count..,
                label = ..count..),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  geom_text(stat = "count",
            aes(x = 1.55, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  theme(legend.position = "none") +
  coord_polar(theta = "y")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/50f74d49-b084-d6f2-0a4b-24e5ed08f517.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/23bf5a8b-8ba4-165b-2134-a1806241c23a.png)

不要な軸等を非表示にします。

```R:R
ggplot(data = iris, aes(fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(reverse = TRUE), color = "black") +
  geom_text(stat = "count",
            aes(x = 1.1, y = ..count..,
                label = ..count..),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  geom_text(stat = "count",
            aes(x = 1.55, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.title = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank())

ggplot(data = iris, aes(fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(reverse = TRUE), color = "black") +
  geom_text(stat = "count",
            aes(x = 1.1, y = ..count..,
                label = ..count..),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  geom_text(stat = "count",
            aes(x = 1.55, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.title = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank()) +
  coord_polar(theta = "y")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/f12b8572-df71-cea6-f72a-0654f5593185.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/b6634a22-de1a-e618-6191-89015fff1a86.png)

```R:R
ggplot(data = iris,
       aes(color = factor(round(Sepal.Length)), fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 1),
           position = position_stack(reverse = TRUE), color = "black", alpha = 0.6) +
  geom_text(stat = "count",
            aes(x = 1.1, y = ..count..,
                label = ..count..), color = "black",
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  geom_text(stat = "count",
            aes(x = 1.6, y = ..count..,
                label = str_c(..fill.., "cm")),
            position = position_stack(vjust = 0.5, reverse = TRUE)) +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.title = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank()) +
  coord_polar(theta = "y")

ggplot(data = iris,
       aes(color = factor(round(Sepal.Length)), fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count",
           aes(x = 2),
           position = position_stack(reverse = TRUE), color = "black", alpha = 0.6,
           width = 2) +
  geom_label(stat = "count",
             aes(x = 3, y = ..count..,
                 label = str_c(..fill.., "cm\n", ..count..)), color = "black",
             position = position_stack(vjust = 0.5, reverse = TRUE)) +
  geom_label(aes(x = 0, y = 0), label = "Total\n150", color = "black", fill = "white") +
  xlim(0, 3) +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        axis.title = element_blank(),
        axis.text  = element_blank(),
        axis.ticks = element_blank()) +
  coord_polar(theta = "y")
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/1f5bab75-916d-ceb9-63d4-fb6b9e35e5ec.png)
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/776f1621-eb90-9b6d-1d7e-ce742665b528.png)



なお、ggpot2を使わずにR標準では`pie()`を使います。

```R:R
table(round(iris$Sepal.Length))
# 
#  4  5  6  7  8 
#  5 47 68 24  6 
pie(table(round(iris$Sepal.Length)),
    labels = str_c(4:8, "cm"),
    clockwise = TRUE,
    init.angle = 90,
    border = TRUE,
    radius = 1.2)
```
![plot.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/800718/9b56f46f-9105-06bc-3c8a-377741f5c131.png)



# 参考

- https://www.jaysong.net/RBook/visualization1.html
- https://www.jaysong.net/RBook/visualization4.html
- https://triadsou.hatenablog.com/entry/20100528/1275042816
- https://qiita.com/tomiyou/items/ffb1bcffe1f9a537929a
- https://qiita.com/hachisukansw/items/3047e516ebdc55c74104
- https://qiita.com/tomiyou/items/ffb1bcffe1f9a537929a
- https://qiita.com/twrcd1227/items/d5d81fe9ddc658865ebb