LoginSignup
0
1

More than 1 year has passed since last update.

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

Last updated at Posted at 2021-07-04

円グラフ

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

この積み上げ棒グラフをもとにします。
(積み上げ棒グラフについてはこちらを参照。)

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

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

R
ggplot(data = iris, aes(x = 1, fill = factor(round(Sepal.Length)))) +
  geom_bar(stat = "count", color = "black") +
  coord_polar(theta = "y")

plot.png

積み上げ棒グラフに値を追加します。
(積み上げ棒グラフに値を追加する方法についてはこちらを参照。)

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
plot.png

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

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
plot.png

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

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
plot.png

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

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
plot.png

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
plot.png

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

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

参考

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1