Home > OS >  How to adjust the label distance with error bar?
How to adjust the label distance with error bar?

Time:02-16

Normally, vjsust = adjust the label distanace with the max y value, which is not beautiful when the sd is different, is it possible to adjust it with an equal relative distance with upper error bar?

In the following example, the a and b should be move down and c up, how to plot it?

df <- data.frame(dose=c("D0.5", "D1", "D2"),
                 len=c(4.2, 10, 29.5), sd = c(0.2, 2, 5),
                 label = c('a', 'b', 'c'))

ggplot(df, aes(x = dose, y = len))  
    geom_errorbar(aes(ymin = len, ymax = len   sd), width=.1, position=position_dodge(.6))  
    geom_bar(position = position_dodge(), stat="identity", width=.4) 
    geom_text(aes(label = label,  angle = 0),vjust = -2.5)

enter image description here

CodePudding user response:

Use the y parameter in the geom_text aestethics to set up the position of the labels according to the error bars:

ggplot(df, aes(x = dose, y = len))  
  geom_errorbar(aes(ymin = len, ymax = len   sd), width=.1, position=position_dodge(.6))  
  geom_bar(position = position_dodge(), stat="identity", width=.4) 
  geom_text(aes(y = len   sd, label = label,  angle = 0),vjust = -2.5)  
  ylim(0, 40)

enter image description here

  • Related