2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ggplotの積み重ね棒グラフで、ラベルを中心に置く

Last updated at Posted at 2019-10-14

目的

  • 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))

グラフ例

Rplot.png

参考

2
0
0

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?