円グラフ
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")
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")
積み上げ棒グラフに値を追加します。
(積み上げ棒グラフに値を追加する方法についてはこちらを参照。)
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")
凡例を非表示にするかわりにグラフに表示します。
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")
積み上げの順を逆にして、円グラフの順を反時計回りにします。
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")
不要な軸等を非表示にします。
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")
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")
なお、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)
参考
- 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