r - 在 y 轴上方添加空间而不用 expand()

标签 r ggplot2 graph

当绘制百分比并且列是 100% 时,值标签会从图表中删除。

enter image description here

对此有两种可能的解决方案:
1. scale_y_continuous(limits = c(0, 1.1)2. scale_y_continuous(expand = c(0, 0, 0.2, 0)但是这两种解决方案都扩展了轴心。我宁愿只添加一个填充/边距,这样我就不会得到超过 100% 的长线。这可能吗?
enter image description here

工作示例

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")

enter image description here

也可能值得注意的是,这只是当你想要一个广泛的情节时的一个问题。

关于r - 在 y 轴上方添加空间而不用 expand(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54004971/

相关文章:

search - lisp 哈希符号替换返回值

algorithm - 图论 - 全局最小割及其含义

r - 使用 R 上的反向传播神经网络模型生成预测会为所有观察返回相同的值

r - 将基本 r 图保存为可以在多图中绘制的对象

java - GraphView不显示点

r - 在 ggplot2 中使用 geom_smooth 绘制虚线回归线

r - 每个单位有轮廓的条形图?

r - 如何使用 stat_contour 完全填充轮廓

R-sqldf-需要明确的单位进行数字转换

r - 在 Shiny 中使用传单绘制多个多边形