Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R ggplot geom_errorbar is not displaying the whiskers

Tags:

r

ggplot2

Plotting my data in R with ggplot, the error bar whiskers are not displayed. Why are the whiskers not displayed and what is the fix so they will be displayed?

(Though it is not necessary to specify "data=..." in geom_errorbar, I am using a smaller set of data to plot a few points and their error bars on top of a larger set of data. I simplified here to just use the smaller data frame for everything but want to keep this example close to the code I intend to use.)

Thanks! Shawna

shapes <- c(1, 19, 15, 1, 0)
names(shapes) <- levels(smallDF$Treatment)

p <- ggplot(data=smallDF, aes(x=pNew, y=diff, group=Treatment))
p <- p + geom_errorbar(data=smallDF, aes(ymin=diff-se,ymax=diff+se),     
color="black", width=.3, position=position_dodge(.5))
p <- p + geom_line(size=.3)
p <- p + geom_point(data=smallDF, aes(shape=Treatment),fill="white",  
                    size=2.5)
#p <- p + scale_shape_manual(values=c(1, 19, 15, 1, 0))
p <- p + scale_shape_manual(values=shapes)
p <- p + xlab("Pressure (mmHg)") + ylab("delD (mm)")
p <- p + theme_bw()
p <- p + theme(
        legend.position="none"
#       , axis.text.y=element_blank()
#       , axis.title.y=element_blank()
        , panel.border=element_blank()
        , axis.line = element_line(colour = "black")
        , axis.text.x = element_text(size=10)
        , axis.text.y = element_text(size=10)
        , axis.title.x= element_text(size=10)
        , axis.title.y= element_text(size=10)
        , strip.text.x = element_text(size = 10)
        )
p

enter image description here

Treatment   step    N   diff    sd  se  ci  predictD    pNew
cntl    2   7   0.256537749 0.130605763 0.049364339 0.120790185 1.483185156 10
cntl    3   7   0.317586245 0.151444256 0.057240549 0.140062577 1.626590815 15
cntl    4   7   0.377309785 0.165262839 0.062463482 0.152842634 1.788401781 20
cntl    5   7   0.433531627 0.173735352 0.065665791 0.160678402 1.964393744 25
cntl    6   7   0.467529177 0.171603123 0.064859884 0.158706419 2.142879696 30
cntl    7   7   0.441401156 0.163740786 0.0618882   0.15143497  2.291729181 35
cntl    8   7   0.360578168 0.143967074 0.054414439 0.133147337 2.402152991 40
cntl    9   7   0.263484929 0.117425017 0.044382485 0.108600028 2.481824239 45
cntl    10  7   0.172079736 0.094209661 0.035607905 0.087129405 2.53504158  50
cntl    11  7   0.081780331 0.070316765 0.026577239 0.065032161 2.561500546 55
cntl    12  7   0.172079736 0.094209661 0.035607905 0.087129405 2.53504158  50
cntl    13  7   0.263484929 0.117425017 0.044382485 0.108600028 2.481824239 45
cntl    14  7   0.360578168 0.143967074 0.054414439 0.133147337 2.402152991 40
cntl    15  7   0.441401156 0.163740786 0.0618882   0.15143497  2.291729181 35
cntl    16  7   0.467529177 0.171603123 0.064859884 0.158706419 2.142879696 30
cntl    17  7   0.433531627 0.173735352 0.065665791 0.160678402 1.964393744 25
cntl    18  7   0.377309785 0.165262839 0.062463482 0.152842634 1.788401781 20
cntl    19  7   0.317586245 0.151444256 0.057240549 0.140062577 1.626590815 15
cntl    20  7   0.256537749 0.130605763 0.049364339 0.120790185 1.483185156 10
3hpx    2   6   0.124643574 0.068765439 0.028073373 0.072164903 1.511618688 10
3hpx    3   8   0.121806932 0.088542241 0.03130441  0.074023166 1.537544183 15
3hpx    4   8   0.138107729 0.097228081 0.034375318 0.08128471  1.602344034 20
3hpx    5   8   0.149529    0.10158369  0.035915258 0.08492609  1.665227481 25
3hpx    6   8   0.157687817 0.101898303 0.036026491 0.085189113 1.724788023 30
3hpx    7   8   0.154261671 0.099724849 0.035258058 0.08337206  1.776475381 35
3hpx    8   8   0.140631071 0.091483297 0.03234423  0.07648195  1.819131367 40
3hpx    9   8   0.123241311 0.083112381 0.029384664 0.069483689 1.854326249 45
3hpx    10  8   0.103092334 0.075467614 0.026681831 0.063092504 1.882440312 50
3hpx    11  8   0.080332775 0.070303352 0.024855989 0.058775073 1.903587298 55
3hpx    12  8   0.103092334 0.075467614 0.026681831 0.063092504 1.882440312 50
3hpx    13  8   0.123241311 0.083112381 0.029384664 0.069483689 1.854326249 45
3hpx    14  8   0.140631071 0.091483297 0.03234423  0.07648195  1.819131367 40
3hpx    15  8   0.154261671 0.099724849 0.035258058 0.08337206  1.776475381 35
3hpx    16  8   0.157687817 0.101898303 0.036026491 0.085189113 1.724788023 30
3hpx    17  8   0.149529    0.10158369  0.035915258 0.08492609  1.665227481 25
3hpx    18  8   0.138107729 0.097228081 0.034375318 0.08128471  1.602344034 20
3hpx    19  8   0.121806932 0.088542241 0.03130441  0.074023166 1.537544183 15
3hpx    20  6   0.124643574 0.068765439 0.028073373 0.072164903 1.511618688 10
like image 472
Shawna Avatar asked Sep 18 '25 16:09

Shawna


1 Answers

I changed your width = 0.3 to width = 5. I also deleted all the redundant data = smallDF that you had and replaced your individually-set font sizes with the base_size argument of theme_bw().

p <- ggplot(data = smallDF, aes(x = pNew, y = diff, group = Treatment)) +
  geom_errorbar(aes(ymin = diff - se, ymax = diff + se),      
                color = "black",
                width = 5,    ## This is the width of the crossbars!
                position = position_dodge(.5))
  geom_line(size = .3)
  geom_point(aes(shape = Treatment),
             fill = "white", size = 2.5) +
  scale_shape_manual(values = shapes) +
  xlab("Pressure (mmHg)") +
  ylab("delD (mm)") +
  theme_bw(base_size = 10) +
  theme(
      legend.position="none"
      , panel.border=element_blank()
      , axis.line = element_line(colour = "black")
  )
p

enter image description here

like image 176
Gregor Thomas Avatar answered Sep 20 '25 08:09

Gregor Thomas