Add space above y-axis without expand()

Hel*_*son 3 r graph ggplot2

When plotting percentages and a column is at 100%, the value label gets cut of from the graph.

在此处输入图片说明

Two possible solutions to this are:
1. scale_y_continuous(limits = c(0, 1.1)
2. scale_y_continuous(expand = c(0, 0, 0.2, 0)
But both solutions expand the axis. I would prefer to just add a padding/margin so that I don't get a long line above 100%. Is this possible? 在此处输入图片说明

Working example

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()
Run Code Online (Sandbox Code Playgroud)

Vic*_*ica 5

You can pad the plot with plot.margin argument in the theme function and turn off clipping in coord_cartesian to allow for drawings to be unconfined to the plot panel.

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")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Probably worth noting as well this is only an issue when you want a wide plot.