Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to adjust the label distance with error bar?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading