简体   繁体   中英

Scale the y axis as power 10 values in R

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..

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');

情节1

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);

情节2

axis() works:

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

在此处输入图片说明

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))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM