条形图上的标签

时间:2019-02-13 17:09:28

标签: r ggplot2

我已经看过很多有关此问题,但似乎没有什么事情做对了。

我有这张足球比赛的条形图,该条形图堆叠在一起以显示哪个队的状态更好或更差,以及如何映射到结果。

我想在条形图的每个分段上添加标签以显示该分段的总数,例如,“ Blades and draw”为15,并且最好在顶部为条形总数。

以下是图表:enter image description here

这是用于创建它的代码:

ggplot(Sheff_derby_form_league, aes(x = Result)) + 
geom_bar(aes(y = ..count.., fill = Res_vs_f_team)) +
labs(title = "Blades vs Owls. League derby form and result",
   subtitle = "Team with form advantage a little more likely to win", 
   x = "Better form: > 0.33ppg over opponent. Even form: < 0.33ppg", y = "") 
   + scale_fill_brewer(palette = "RdBu")

1 个答案:

答案 0 :(得分:0)

由于您尚未提供数据,因此我使用的是默认数据集之一。您可以使用ggbarstatshttps://indrajeetpatil.github.io/ggstatsplot/reference/ggbarstats.html)中的ggstatsplot函数轻松制作带标签的堆积条形图,该函数还提供了一些其他有用的细节。如下面的多个示例所示,可以根据自己的喜好修改默认值。

# setup
set.seed(123)
library(ggplot2)
library(ggstatsplot)

# percentage
ggstatsplot::ggbarstats(
  data = mtcars,
  main = am,
  condition = cyl,
  messages = FALSE
)

# count
ggstatsplot::ggbarstats(
  data = mtcars,
  main = am,
  condition = cyl,
  data.label = "count",
  package = "wesanderson",
  palette = "Royal2",
  messages = FALSE
)

# count and percentage
ggstatsplot::ggbarstats(
  data = mtcars,
  main = am,
  condition = cyl,
  data.label = "both",
  messages = FALSE
) +
  ggplot2::labs(subtitle = NULL)

reprex package(v0.2.1)于2019-02-13创建