Change Boxplot Axis Labels

13,007

The problem seems to be that your boxplots are based on log10 of value, whereas the axis you are drawing uses the original values. There are two ways to fix this. Either use log10 to generate your axis ticks, or else use log="y" when you generate your boxplots to do the coordinate transformation. Here is an illustration with some sample data:

set.seed(123)
x<-sample(100,1000,T)
var<-sample(letters[1:5],1000,T) 

Choice number 1:

boxplot(log10(x) ~ var,yaxt="n")
axis(side=2,labels=round(10^(seq(log10(min(x)),log10(max(x)),len=5)),2),at=seq(log10(min(x)),log10(max(x)),len=5))

enter image description here

Choice number 2:

boxplot(x ~ var,yaxt="n",log="y")
axis(side=2,labels=seq(min(x),max(x),len=5),at=seq(min(x),max(x),len=5))

enter image description here

You can get the ticks to be spaced logarithmically or linearly with either method by specifying the at parameter appropriately, for example, this command will place evenly spaced tick marks on a plot generated with log="y":

 axis(side=2,labels=round(exp(seq(log(min(x)),log(max(x)),len=5)),2),at=exp(seq(log(min(x)),log(max(x)),len=5)))
Share:
13,007
Komal Rathi
Author by

Komal Rathi

My primary goal is to analyze large scale genomic data, develop bioinformatics methods and build analysis and visualization tools in order to empower cancer research that can clinically be translated to therapy.

Updated on June 14, 2022

Comments

  • Komal Rathi
    Komal Rathi almost 2 years

    I have a data frame mdata which has the columns variable and value that I am plotting on a BoxPlot in R. I am plotting log10(value) on the Y-axis & variables on the X-axis. I want to change the labels on the Y-axis such that it shows the original values and not the log10(value).

    >mdata
    
       ID          variable value
      SJ5444_MAXGT   coding 17455
      SJ5426_MAXGT   coding 17961
      HR1383_MAXGT   coding 17579
      HR5522_MAXGT   coding 17797
     CH30041_MAXGT   coding 20099
      SJ5438_MAXGT   coding 17467
    

    I want the Y-axis range to go from min(mdata$value) to max(mdata$value) with an interval of 10000. But I am unable to do so.

    The following is my code:

    boxplot(log10(as.numeric(value))~variable,data=mdata,yaxt="n",border="red",main="Boxplot: Seattle Seq Annotation")
    
    axis(side=2,labels=seq(min(mdata$value),max(mdata$value),10000),cex.axis=0.65,tck=-0.02,at=seq(min(mdata$value),max(mdata$value),by=10000))
    

    I have tried to figure out what is the problem here but its not obvious. Any help would be appreciated.