Home > database >  R - Change position of geom_text inside geom_bar
R - Change position of geom_text inside geom_bar

Time:12-04

I am trying to control the position of two calls of geom_text inside geom_bar, but I just can't get it right.

This is what I have so far:

enter image description here

However, I would like to change two things in this plot:

  1. Put the letters (first geom_text call) at the top left corner of the bars, just before the error bars. Or perhaps on top of the error bars.
  2. Put the numbers (second geom_text call) in the middle of the bars.

Here are my code and dataset sample:

library(ggplot2)

dat <- structure(list(ObservationId = c("Control", "Control", "Control", 
"Control", "Control", "Control", "Control", "Control", "Control"
), Treatment = c("TREATMENT A", "TREATMENT B", "UNTREATED", "TREATMENT A", 
"TREATMENT B", "UNTREATED", "TREATMENT A", "TREATMENT B", "UNTREATED"
), day_class = c("Time1", "Time1", "Time1", "Time2", "Time2", 
"Time2", "Time3", "Time3", "Time3"), Estimate = c(100, 99.8, 
0.7, 97.2, 91.2, 7.2, 94.6, 87.3, 14.5), SE = c(4.2, 4.4, 3.7, 
3.6, 4.1, 3.8, 3.7, 4.1, 3.8), df = c(38.5, 42.3, 37.4, 33.5, 
37.4, 37.8, 33.9, 38.5, 38.1), lower.CL = c(91.6, 90.9, -6.9, 
89.8, 83, -0.4, 87.2, 79, 6.9), upper.CL = c(108.4, 108.7, 8.3, 
104.6, 99.5, 14.8, 102.1, 95.7, 22.1), Class = c("A", "A", "B", 
"A", "A", "B", "A", "A", "B")), row.names = c(NA, 9L), class = "data.frame")

ggplot(dat,
       aes(x = Treatment,
           y = Estimate,
           group = day_class,
           fill = day_class))  
  theme_bw()  
  geom_col(position = "dodge", color = "black")  
  geom_errorbar(aes(ymin = lower.CL,
                    ymax = upper.CL),
                width = 0.2, position = position_dodge(0.9))  
  geom_text(aes(label = Class, group = day_class),
            position = position_dodge(width = 0.9),
            color = 'blue',
            size = 5)  
  geom_text(aes(label = round(Estimate,1), group = day_class),
            position = position_dodge(width = 0.9),
            size = 4) 

How can I achieve those changes?

CodePudding user response:

You can use y inside geom_text, an example:

ggplot(dat,
       aes(x = Treatment,
           y = Estimate,
           group = day_class,
           fill = day_class))  
  theme_bw()  
  geom_col(position = "dodge", color = "black")  
  geom_errorbar(aes(ymin = lower.CL,
                    ymax = upper.CL),
                width = 0.2, position = position_dodge(0.9))  
  geom_text(aes(label = Class, group = day_class,y = upper.CL),
            position = position_dodge(width = 0.9),
            color = 'blue',
            size = 5)  
  geom_text(aes(label = round(Estimate,1), group = day_class, y = (upper.CL Estimate)/2 ),
            position = position_dodge(width = 0.9),
            size = 4) 

enter image description here

CodePudding user response:

hjust and vjust offers flexibility to nudge labels:

<as your code>  
     geom_text(aes(label = Class, group = day_class),
                position = position_dodge(width = 0.9),
                color = 'blue',
                hjust = 4, vjust = -3,
                size = 5)                  
      geom_text(aes(label = round(Estimate,1), group = day_class),
                position = position_dodge(width = 0.9),
                hjust = 0.5, vjust = 2,
                size = 4) 

enter image description here

  • Related