1
2

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 3 years have passed since last update.

ggplot2で棒グラフに値を追加する方法メモ

Last updated at Posted at 2021-07-04

棒グラフに数値を入れる

棒グラフの並べ方はpositionで指定できます。
参考:https://qiita.com/swathci/items/b08496d863bca4b479b3#グラフの色分け

  • position = position_stack(), "stack":積み上げ棒グラフ
  • position = position_fill(), "fill":100%積み上げ縦棒
  • position = position_identity(), "identity":棒グラフの重ね合わせ
  • position = position_dodge(), "dodge":横並びの棒グラフ
  • position = position_dodge2(), "dodge2":横並びの棒グラフ

それぞれの場合について、geom_text()で数値を入れてみたいと思います。

積み上げ棒グラフ:position = position_stack(), "stack"

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_stack(), alpha = 1/3)

plot.png

このグラフに数値を入れます。

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack())
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_stack(), alpha = 1/3) +
  stat_count(geom = "text", aes(label = ..count..),
             position = position_stack())

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 1))  # vjust = 1:デフォルト

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 0.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_stack(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_stack(vjust = 0.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_stack(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(vjust = 0.5), show.legend = FALSE)

plot.png
plot.png
plot.png
plot.png

reverse = TRUE

R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_stack(reverse = TRUE), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_stack(reverse = TRUE, vjust = 0.5), show.legend = FALSE)

plot.png

100%積み上げ縦棒:position_fill(), "fill"

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_fill(), alpha = 1/3)

plot.png

このグラフに数値を入れます。

R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(vjust = 1))  # vjust = 1:デフォルト

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(vjust = 0.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_fill(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_fill(vjust = 0.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_fill(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(vjust = 0.5), show.legend = FALSE)

plot.png
plot.png
plot.png

reverse = TRUE

R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_fill(reverse = TRUE), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_fill(reverse = TRUE, vjust = 0.5), show.legend = FALSE)

plot.png

棒グラフの重ね合わせ:position_identity(), "identity"

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_identity(), alpha = 1/3)

plot.png

このグラフに数値を入れます。

R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_identity())

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..+1.5),
            position = position_identity())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = 0, y = +1.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..-1.5),
            position = position_identity())
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = 0, y = -1.5))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_nudge(x = 0, y = +1.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_identity(), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = 0, y = +1.5), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_identity(), alpha = 1/5, width = 0.6) +
  geom_text(stat = "count", aes(label = ..count.., color = Species),
            position = position_nudge(x = -0.4, y = +1.5), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_identity(), alpha = 1/5, width = 0.6) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_nudge(x = -0.4, y = +1.5), show.legend = FALSE)

plot.png
plot.png
plot.png
plot.png
plot.png

横並びの棒グラフ:position_dodge(), "dodge"

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_dodge(), alpha = 1/3)

ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "single"), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_dodge(preserve = "single"), alpha = 1/3)

plot.png
plot.png

このグラフに数値を入れます。

R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(width = 0.9), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_dodge(width = 0.9))
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_dodge(preserve = "total", width = 0.9))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count..+1.5),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..+1.5),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count../2),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge(preserve = "total", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count../2),
            position = position_dodge(preserve = "total", width = 0.9), show.legend = FALSE)

plot.png
plot.png
plot.png

横並びの棒グラフ:position_dodge2(), "dodge2"

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single"), alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar", aes(color = Species, fill = Species),
             position = position_dodge2(preserve = "single"), alpha = 1/3)

plot.png

このグラフに数値を入れます。

R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/3) +
  geom_text(stat = "count", aes(label = ..count..),
            position = position_dodge2(preserve = "single", width = 0.9))

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count..+1.5),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count..+1.5),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count", aes(color = Species, fill = Species),
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., color = Species, y = ..count../2),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge2(preserve = "single", width = 0.9), alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count../2),
            position = position_dodge2(preserve = "single", width = 0.9), show.legend = FALSE)

plot.png
plot.png
plot.png

reverse = TRUE

R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_dodge2(preserve = "single", width = 0.9, reverse = TRUE),
           alpha = 1/5) +
  geom_text(stat = "count", aes(label = ..count.., y = ..count../2),
            position = position_dodge2(preserve = "single", width = 0.9, reverse = TRUE),
            show.legend = FALSE)

plot.png

横並びの棒グラフでグループごとの位置をそろえる

横並びのposition = position_dodge(), position = position_dodge2()では、x軸ごとにグループの数が異なる場合はグループの位置がそろいません。
グループの位置をそろえるには、position = position_identity()で直接的に位置を指定すればよさそうです。

R
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length))) +
  stat_count(geom = "bar",
             aes(color = Species, fill = Species,
                 x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
             position = position_identity(), width = 0.3, alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/3)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  stat_count(geom = "bar",
             aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
             position = position_identity(), width = 0.3, alpha = 1/3)

p <- ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count")
layer_data(p)
#     y count prop x flipped_aes group PANEL ymin ymax xmin xmax colour   fill size linetype alpha
# 1   5     5 0.10 4       FALSE     1     1    0    5 3.55 4.45     NA grey35  0.5        1    NA
# 2  47    40 0.80 5       FALSE     1     1    7   47 4.55 5.45     NA grey35  0.5        1    NA
# 3  68     5 0.10 6       FALSE     1     1   63   68 5.55 6.45     NA grey35  0.5        1    NA
# 4   7     6 0.12 5       FALSE     2     1    1    7 4.55 5.45     NA grey35  0.5        1    NA
# 5  63    36 0.72 6       FALSE     2     1   27   63 5.55 6.45     NA grey35  0.5        1    NA
# 6  24     8 0.16 7       FALSE     2     1   16   24 6.55 7.45     NA grey35  0.5        1    NA
# 7   1     1 0.02 5       FALSE     3     1    0    1 4.55 5.45     NA grey35  0.5        1    NA
# 8  27    27 0.54 6       FALSE     3     1    0   27 5.55 6.45     NA grey35  0.5        1    NA
# 9  16    16 0.32 7       FALSE     3     1    0   16 6.55 7.45     NA grey35  0.5        1    NA
# 10  6     6 0.12 8       FALSE     3     1    0    6 7.55 8.45     NA grey35  0.5        1    NA
layer_data(p)$group
# [1] 1 1 1 2 2 2 3 3 3 3
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_nudge(x = (layer_data(p)$group-2)*0.9/3, y=0),
           width = 0.3, alpha = 1/3)

plot.png

このグラフに数値を入れます。

R
ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/3) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count.., color = Species,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count.., color = Species,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count../2),
            position = position_identity(), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count../2),
            position = position_identity(), show.legend = FALSE)

ggplot(data = iris, aes(x = round(Sepal.Length), group = Species)) +
  geom_bar(stat = "count",
           aes(color = Species, fill = Species,
               x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count.., color = Species,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3,
                y = ..count..-..count..+1.5),
            position = position_identity(), show.legend = FALSE)
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3,
                y = ..count..-..count..+1.5),
            position = position_identity(), show.legend = FALSE)

plot.png
plot.png
plot.png
plot.png

reverse

R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           aes(x = round(Sepal.Length) + (2-as.integer(Species))*0.9/3, y = ..count..),
           position = position_identity(), width = 0.3, alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (2-as.integer(Species))*0.9/3, y = ..count../2),
            position = position_identity(), show.legend = FALSE) +
  xlim(4-0.9*3/6, 8+0.9*3/6)

plot.png

R
ggplot(data = iris, aes(x = round(Sepal.Length), color = Species, fill = Species)) +
  geom_bar(stat = "count",
           position = position_identity(), alpha = 1/5) +
  geom_text(stat = "count",
            aes(label = ..count..,
                x = round(Sepal.Length) + (as.integer(Species)-2)*0.9/3, y = ..count..+1.5),
            position = position_identity(), show.legend = FALSE)

plot.png

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?