简体   繁体   English

将 y 轴缩放为 R 中的 10 次幂值

[英]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.我试图在 y 轴上只获得 10 次幂,并且我尝试了 StackOverflow 上提供的许多方法,但都没有产生我想要的。

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.我想定义 y 轴的比例从 10^2 开始,然后下一个刻度是 10^4,然后是 10^6。

I have tried xaxt and at and axis and everything mentioned here , but none work for 10^x..我已经尝试过xaxtataxis以及这里提到的所有内容,但没有一个适用于 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. plot.default()函数提供了一个log参数,可用于轻松获取 x 轴、y 轴或两者的对数刻度。 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:如果要控制 y 轴刻度,可以覆盖默认轴并以正常方式绘制自己的轴:

## 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: axis()工作:

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??你知道1e+n增长有多快吗?? As you have already seen in the plot, the ticks for 1e+2 and 1e+4 almost coincide, because 1e+6 is so big.正如您在图中已经看到的,1e+2 和 1e+4 的刻度几乎重合,因为 1e+6 太大了。 If your data range from 1 ~ 1e+8 or even greater, then you'd better plot them on log scale.如果您的数据范围从1 ~ 1e+8甚至更大,那么您最好在对数刻度上绘制它们。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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