当绘制百分比并且列是 100% 时,值标签会从图表中删除。
对此有两种可能的解决方案:
1. scale_y_continuous(limits = c(0, 1.1)
2. scale_y_continuous(expand = c(0, 0, 0.2, 0)
但是这两种解决方案都扩展了轴心。我宁愿只添加一个填充/边距,这样我就不会得到超过 100% 的长线。这可能吗?
工作示例
library(ggplot2)
library(magrittr)
data.frame("value" = c(0, 0.5, 1),
"v1" = letters[1:3]) %>%
ggplot(aes(x = v1,
y = value,
label = value)) +
geom_bar(stat = "identity") +
geom_text(stat = "identity",
vjust = -1) +
scale_y_continuous(breaks = seq(0, 1, 0.2),
limits = c(0, 1),
labels = scales::percent,
expand = c(0, 0, 0.2, 0)) +
theme_classic()
最佳答案
您可以使用 plot.margin
填充绘图theme
中的参数功能并关闭剪辑 coord_cartesian
允许图纸不受绘图面板的限制。
data.frame("value" = c(0, 0.5, 1),
"v1" = letters[1:3]) %>%
ggplot(aes(x = v1,
y = value,
label = value)) +
geom_bar(stat = "identity") +
geom_text(stat = "identity",
vjust = -1) +
scale_y_continuous(breaks = seq(0, 1, 0.2),
limits = c(0, 1),
labels = scales::percent) +
theme_classic() +
theme(plot.margin = margin(t = 10, unit = "pt")) + ## pad "t"op region of the plot
coord_cartesian(clip = "off")
也可能值得注意的是,这只是当你想要一个广泛的情节时的一个问题。
关于r - 在 y 轴上方添加空间而不用 expand(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004971/