Search code examples
rplotplotrix

How to flip y-axis (positive to negative) without losing error bars with plotrix in R


I'm trying to make a graph using the package plotrix in R and I'm having trouble getting it to display the way I would like. Due to how the data is interpreted, positive numbers actually mean that the individuals in that group performed worse and negative numbers mean they performed better. This is confusing when the y-axis is plotted normally (increasing numbers as you go up), but might be easier to interpret if the y-axis was flipped.

I was able to flip the y-axis values and the points plot correctly, but I lose my error bars in the process and I'm not sure why. Here is some example code that demonstrates the issue:

Plot with "normal" y-axis (increasing numbers as you go up):

#rm(list=ls(all=TRUE))  
library(plotrix)  
longxlim <- c(0,4)  
longylim <- c(-2,2)  
plotCI(x = c(1:3), 
   y = c(-1:1), 
   uiw = c(0.25, 0.5, 0.75),
   xlim = longxlim, ylim = longylim,
   pch = c(1:3))

Plot with "flipped" y-axis (decreasing numbers as you go up):

longxlim <- c(0,4)
longylim <- c(2,-2)
plotCI(x = c(1:3), 
   y = c(-1:1), 
   uiw = c(0.25, 0.5, 0.75),
   xlim = longxlim, ylim = longylim,
   pch = c(1:3))

Solution

  • instead of negating the limits, maybe negate the y-values themselves?

    library(plotrix)  
    longxlim <- c(0,4)  
    longylim <- c(-2,2)  
    yplot <- c(-1:1)
    
    
    plotCI(x = c(1:3), 
           y = -yplot, 
           uiw = c(0.25, 0.5, 0.75),
           xlim = longxlim, ylim = longylim,
           pch = c(1:3))
    

    To change the y-axis labels you can set yaxt = 'n' and use axis

    longxlim <- c(0,4)  
    longylim <- c(-2,2)  
    yplot <- c(-1:1)
    ylabs <- c(-2:2)
    
    plotCI(x = c(1:3), 
           y = -yplot, 
           uiw = c(0.25, 0.5, 0.75),
           xlim = longxlim, ylim = longylim,
           pch = c(1:3), yaxt = 'n')
    axis(2, at = ylabs, labels = -ylabs)
    

    enter image description here