0

Apologies if this question is trivial, I've created a side geom_bar plot and I'm just trying to figure out how I can adjust the positioning of the percentage labels. I would like to have each label either just outside each sidebar, or just inside (but not overlapping inside and outside like in the image below)

Here is my dataset bars

structure(list(date = structure(c(18577, 18577, 18577, 18577, 
18577, 18577, 18577, 18577, 18577, 18577), class = "Date"), Ticker = c("ARS", 
"BRL", "IDR", "INR", "MXN", "NGN", "RUB", "THB", "TRY", "ZAR"
), Return = c(-7.1, 2.1, 3.5, -0.8, 7.3, 1.3, -3.3, 2.5, -10.5, 
8)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

and my code below

p1 <- ggplot(bars, aes(Ticker,Return,fill=Ticker)) +
  geom_bar(stat='identity') +
  coord_flip() +
  theme(legend.position = 'bottom') +
  labs(title = '% Change in EM Currencies vs USD since September 1',
       subtitle = 'Performance of EM Currencies has diverged recently',
       y = '% Change since September 1', x = '') +
  geom_hline(yintercept = 0,linetype = 'dashed',
             col = 'black',size = 1) +
  geom_text(aes(label=paste0(Return,"%")))


p1

enter image description here

Tanga94
  • 695
  • 6
  • 27
  • [look here](https://stackoverflow.com/questions/46300666/put-labels-over-negative-and-positive-geom-bar), this is very similar problem, it can mostly be controlled within your `geom_text` function – Daniel_j_iii Nov 11 '20 at 12:26

2 Answers2

0

You can add the hjust argument in geom_text

geom_text(aes(label=paste0(Return,"%"), hjust="left"))
Ashish
  • 337
  • 3
  • 11
0

My method is slightly less efficient than the other answer but still gets the job done. I created two conditional geom_text calls and adjusted hjust to get the values outside of the bars. Also had to expand the limits a little bit to make room for the labels on the tallest bars.

library(tidyverse)

bars <- structure(list(date = structure(c(18577, 18577, 18577, 18577, 
                                  18577, 18577, 18577, 18577, 18577, 18577), class = "Date"), 
               Ticker = c("ARS","BRL", "IDR", "INR", "MXN", "NGN", "RUB", "THB", "TRY", "ZAR"), 
               Return = c(-7.1, 2.1, 3.5, -0.8, 7.3, 1.3, -3.3, 2.5, -10.5, 8)), 
          row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

position = position_dodge(width = -.75)
width =-.75
                                                                                           
p1 <- ggplot(bars, aes(Ticker,Return,fill=Ticker)) +
  geom_bar(stat='identity') +
  coord_flip() +
  theme(legend.position = 'bottom') +
  labs(title = '% Change in EM Currencies vs USD since September 1',
       subtitle = 'Performance of EM Currencies has diverged recently',
       y = '% Change since September 1', x = '') +
  geom_hline(yintercept = 0,linetype = 'dashed',
             col = 'black',size = 1) +
  geom_text(aes(label=ifelse(Return < 0, paste0(Return,"%"), "")), hjust = 1.1) +
  geom_text(aes(label=ifelse(Return > 0, paste0(Return,"%"), "")), hjust = -0.2) +
  expand_limits(y = -12, x = 12)


p1

Created on 2020-11-11 by the reprex package (v0.3.0)

Jeffrey Brabec
  • 481
  • 6
  • 11