Search code examples
rplotaxis-labels

Moving down and keeping all enlarged axis number labellings of a plot in r


I have a plot with a logarithmic scale:

png("test.png")
set.seed(20)
x = 1:100
y = sample(100)
plot(x,y,log="x",xaxt='n',cex.axis=2)
axis(1,cex.axis=4)
dev.off()

test.png yields:

enter image description here

I want 2 things,

  1. Move the axis numbers down so that they don't overlap the axis and it's ticks
  2. Keep the disappeared numbers "2", "10", "50" even if they overlap with the other numbers. (The axis numbers should not reduce in size!)

Thank you.


Solution

  • Something like this? We can use mapply() which calls axis() once for each tick location returned by axTicks(). Doing this allows you to keep the missing values that are not shows to avoid over plotting. Also, I threw in some padding with padj so your values aren't entering into the plot itself.

    png("test.png")
    set.seed(20)
    x = 1:100
    y = sample(100)
    
    
    plot(x,y,log="x",xaxt='n',cex.axis=2)
    mapply(axis, 
           side = 1, 
           at = axTicks(1), 
           labels = axTicks(1), 
           cex.lab=4, 
           cex.axis=4, 
           cex.main=4, 
           cex.sub=4, 
           padj = 0.5)
    

    enter image description here