3

I am trying to get only power 10 scale on the y axis, and I have tried many approaches offered on StackOverflow but none produce what I want.

I want to define the scale of y-axis to start from 10^2 and then next tick be at 10^4 and then on 10^6.

I have tried xaxt and at and axis and everything mentioned here, but none work for 10^x..

2 Answers2

4

The plot.default() function provides a log argument which can be used to easily get a logarithmic scale of the x-axis, y-axis, or both. For example:

x <- 2:6;
y <- 10^x;
plot(x,y,log='y');

plot1

If you want to control the y-axis ticks, you can override the default axis and draw your own in the normal way:

## generate data
x <- 2:6;
y <- 10^x;

## precompute plot parameters
xlim <- c(2,6);
ylim <- c(10^2,10^6);
xticks <- 2:6;
yticks <- 10^seq(2L,6L,2L);

## draw plot
plot(NA,xlim=xlim,ylim=ylim,log='y',axes=F,xaxs='i',yaxs='i',xlab='x',ylab='y');
abline(v=xticks,col='lightgrey');
abline(h=yticks,col='lightgrey');
axis(1L,xticks,cex.axis=0.7);
axis(2L,yticks,sprintf('10^%d',as.integer(log10(yticks))),las=2L,cex.axis=0.7);
##axis(2L,yticks,sprintf('%.0e',yticks),las=2L,cex.axis=0.7); ## alternative
points(x,y,pch=19L,col='red',xpd=NA);

plot2

bgoldst
  • 34,190
  • 6
  • 38
  • 64
1

axis() works:

x <- 2:6
y <- 10 ^ x
plot(x, y, yaxt = "n")
axis(2, at = 10^(c(2, 4, 6)))

enter image description here

The only problem is that, this is certainly not a good way for presentation. Do you know how fast 1e+n grows?? As you have already seen in the plot, the ticks for 1e+2 and 1e+4 almost coincide, because 1e+6 is so big. If your data range from 1 ~ 1e+8 or even greater, then you'd better plot them on log scale.

plot(x, log10(y), yaxt = "n")
axis(2, at = c(2,4,6))

enter image description here

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248