Search code examples
rplotaxesplotrix

Error bars are not plotted when axes are reversed (plotCI from package plotrix)


I want to change the point of origin for a plot with vertical and horizontal error bars. I'm using the plotCI function from the 'plotrix' package for plotting.

A very short reproducible example:

x <- c(1, 2)
y <- c(3, 4)
err.x <- c(0.5, 0.2)
err.y <- c(0.25, 0.3)
plotCI(x, y, uiw = err.x, err = "x",
       ylim = range(y+err.y, y-err.y))
plotCI(x, y, uiw = err.y, err = "y", add = T)

Everything is fine in this plot. I got both horizontal and vertical error bars.

plotCI(x, y, uiw = err.x, err = "x",
       ylim = rev(range(y+err.y, y-err.y)))
plotCI(x, y, uiw = err.y, err = "y", add = T)

Here, I only get the horizontal error bars. It seems as if the reversal of the y-axis wasn't 'recognized' by the second call to plotCI.

Any ideas?!? Thanks a lot!


Solution

  • I love plotrix and its associated functions but I think what you're trying to do is hampered by the arrows() function that plotCI() relies on not honoring the ylim reversal. You can instead use ggplot2 to get the plot you want:

    x <- c(1, 2)
    y <- c(3, 4)
    err.x <- c(0.5, 0.2)
    err.y <- c(0.25, 0.3)
    
    library(ggplot2)
    ggplot(data.frame(x,y,err.x,err.y), aes(x=x, y=y)) +
      geom_point() + 
      geom_errorbar(aes(ymin=y-err.y, ymax=y+err.y), width=0.05) + 
      geom_errorbarh(aes(xmin=x-err.x, xmax=x+err.x), height=0.05) + 
      scale_y_reverse()
    

    enter image description here