我已经看过很多有关此问题,但似乎没有什么事情做对了。
我有这张足球比赛的条形图,该条形图堆叠在一起以显示哪个队的状态更好或更差,以及如何映射到结果。
我想在条形图的每个分段上添加标签以显示该分段的总数,例如,“ Blades and draw”为15,并且最好在顶部为条形总数。
这是用于创建它的代码:
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")
答案 0 :(得分:0)
由于您尚未提供数据,因此我使用的是默认数据集之一。您可以使用ggbarstats
(https://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创建