百分比标签不显示在条形图上
Percentage labels are not displayed over bar plot
我正在尝试绘制带有百分比标签的条形图。然而,令人恼火的是,百分比并未显示在每个相应栏的上方,而是以您在下方看到的方式放置。有谁知道是什么原因造成的以及如何解决?
我使用的代码是:
p1 <- ggplot(mtcars, aes(x= cyl)) +
geom_bar(aes(fill = vs), stat = "count") +
geom_text(aes(label = scales::percent(..prop..), y= ..prop..), stat = "count", vjust = -0.5) +
theme_classic() + ylab("Count") + facet_grid(vs ~ .)
这给出了
请注意,我想在 y 轴上计数。
我们可以使用ymax
和vjust
:
library(ggplot2)
ggplot(mtcars, aes(x= cyl)) +
geom_bar(aes(fill = vs), stat = "count") +
geom_text(aes(ymax= ..prop.., label = scales::percent(..prop..)), stat = "count", vjust = -.1) +
theme_classic() +
ylab("Count") +
facet_grid(vs ~ .)
我正在尝试绘制带有百分比标签的条形图。然而,令人恼火的是,百分比并未显示在每个相应栏的上方,而是以您在下方看到的方式放置。有谁知道是什么原因造成的以及如何解决?
我使用的代码是:
p1 <- ggplot(mtcars, aes(x= cyl)) +
geom_bar(aes(fill = vs), stat = "count") +
geom_text(aes(label = scales::percent(..prop..), y= ..prop..), stat = "count", vjust = -0.5) +
theme_classic() + ylab("Count") + facet_grid(vs ~ .)
这给出了
请注意,我想在 y 轴上计数。
我们可以使用ymax
和vjust
:
library(ggplot2)
ggplot(mtcars, aes(x= cyl)) +
geom_bar(aes(fill = vs), stat = "count") +
geom_text(aes(ymax= ..prop.., label = scales::percent(..prop..)), stat = "count", vjust = -.1) +
theme_classic() +
ylab("Count") +
facet_grid(vs ~ .)