0

I need to put labels on top/bottom of the bars as my numbers are both positive and negative (=both sides of x axe).

I have been trying to use vjust/hjust with no success so far....

labels

total_merged%>%
      ggplot(aes(factor(NAPLAN_YEAR), value_add_sector_total, fill = model,
                 label = value_add_sector_total
               )) +
      geom_col(position = position_dodge(width = 0.75), width = 0.5) +
      facet_grid(.~sector_report, switch = 'x') +
      scale_fill_manual(NULL, values = c('#1e3763', '#bebebe'), labels = c("Base", "MLSH")) +
      theme_minimal() +
      geom_text(position = position_dodge(width = 0.75), hjust=0.5, vjust = 1.7, #angle = 90, 
                color="darkred")+
          
      labs(title="",
           x ="", y="System 'value add'")+
      theme(legend.position = 'bottom',
          aspect.ratio = 4/3,
            strip.placement = 'outside',
            panel.spacing.x = unit(0, 'mm'),
            axis.title.x = element_blank(),
            panel.grid.major.x = element_blank(),
            panel.grid.minor.y = element_blank(),
            strip.text = element_text(face = 2),
            legend.text=element_text(size=9),
            legend.title=element_text(size=9),
            axis.text= element_text(size = 9, colour = "black")
      )

My data looks like this (first 50):

structure(list(value_add_sector_total = c(-1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, -1.38, 
-1.38, -1.38), NAPLAN_YEAR = c(2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011), model = c("base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base", "base", "base", "base", "base", 
"base", "base", "base", "base")), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))
m45ha
  • 399
  • 1
  • 9

1 Answers1

2

I can't replicate your code with the data that you provided so I will use a simple data frame.

Using vjust() inside aes() we can use the values from our data.frame without the need to create dummy data:

data.frame(V1 = c(20,-30,-40,50), V2 = letters[1:4], V3 = c("A","A","B","B")) |> 
  ggplot(aes(V2, V1, fill = V3)) + 
  geom_col() +
  geom_text(aes(label = V1, vjust = ifelse(V1 > 0, 0, 1)) )

enter image description here

M Aurélio
  • 830
  • 5
  • 13
  • thank you! that worked! is there any guidance on how exactly vjust and hjust work? 'You can modify text alignment with the vjust and hjust aesthetics. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character ("left", "middle", "right", "bottom", "center", "top"). " But how 0.7 translates to normal language? or -0.7? if i need the number to have some space between the bar - should i use padding or vjust? – m45ha Feb 01 '23 at 21:04
  • vjust and hjust behavior well defined for values 0, 0.5 and 1, anything else is a bit odd. You could read more about it this answer https://stackoverflow.com/questions/7263849/what-do-hjust-and-vjust-do-when-making-a-plot-using-ggplot – M Aurélio Feb 03 '23 at 13:52