0

When I place the values of bars of a barplot with geom_text on top of the bars, the values are cutted and not fully visible. How can I increase the "height" of the plot area so that the values are fully shown?

library(dplyr)
library(ggplot2)
library(ggthemes)

df <- tibble(
  Wer = c('Abt1', 'Abt2'),
  Einnahmen = c(221319.85, 9835.18)
)

df %>% 
  ggplot(aes(x=Wer, y=Einnahmen, fill=Wer)) +
  geom_col(fill=c('darkblue', 'darkred'), width=0.6) +
  geom_text(aes(label=scales::comma(Einnahmen, big.mark = '.', decimal.mark = ',', prefix = '\u20AC ')), vjust=-0.1, color="black", size=4) +
  ggtitle('Einnahmen') +
  scale_y_continuous(expand = expansion(mult = c(0, 0.1), add = c(0, 0))) +
  scale_y_continuous(labels = scales::label_dollar(prefix='€', suffix='k', scale=0.001, big.mark='.', decimal.mark=',')) +
  theme_minimal() +
  theme(plot.title = element_text(size = 16)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text = element_text(size = 12)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank())

bar plot

I played around with scale_y_continuous and expand, but there are no effects.

Edit: I found out that when I use scale_y_continuous to set the number formats to include the Euro symbol and the k for thousands the expand does not work. Is there a workaround to use the bars values instead of the labels?

Joysn
  • 987
  • 1
  • 16
  • 30
  • 1
    Difficult to debug your code without your code mate – Chamkrai May 28 '23 at 21:18
  • 2
    `coord_cartesian(clip = "off")` will remove the clipping at the edge of the plot range. – Jon Spring May 28 '23 at 21:34
  • Possible duplicate except x axis rather than y axis:: https://stackoverflow.com/questions/75098826/automatically-leave-enough-room-for-a-label-next-to-a-barplot/75099898 – Allan Cameron May 28 '23 at 21:48
  • 1
    But if you add `limits = c(0, 250000)` inside `scale_y_continuous` your labels will fit just fine. – Allan Cameron May 28 '23 at 21:50
  • @Chamkrai I added a MWE. But I had this snippet within a Rmd file and I played with fig.height on the R snippet. And with fig.height=3 it the text was cutted, with 4 it fits. I thought that the fig.height scales down the whole figure properly. – Joysn May 28 '23 at 21:50
  • @JonSpring thank you, `coord_cartesian(clip = "off")` worked, I can reduce `fig.height`again to 3 and the text is still shown fully. – Joysn May 28 '23 at 21:58
  • @AllanCameron I want to avoid setting limits, as the max values on the y axis are dynamic. Well, I could extract them upfront... But I will stick to setting clip to off as by Jon's advice. Anyway thank you :) – Joysn May 28 '23 at 22:02
  • 2
    The positioning of your elements, including the limit of what gets clipped, will vary with the resolution of your image. You can increase the shown range using `limits` or `expand` inside `scale_y_continuous`, and/or turn off clipping. More here: https://www.christophenicault.com/post/understand_size_dimension_ggplot2 – Jon Spring May 28 '23 at 22:04

0 Answers0