目的
- ggplotの積み重ね棒グラフで、ラベルを各々の積み重ねの中心に置く
- 日本語での検索に引っかかるようにする
内容
- geom_text()を使用
- position = position_stack(vjust = 0.5)で位置調整
- groupの使用しないと場所がずれる
- geom_col()を使えば、geom_bar()でのstat = "identity"は不要
プログラム例
library(tidyverse)
Year <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data <- tibble(Year, Category, Frequency)
Data %>%
ggplot(aes(x = Year, y = Frequency)) +
geom_col(aes(fill = Category), color = "black") +
geom_text(aes(label = Frequency, group = Category),
size = 6,
position = position_stack(vjust = 0.5))