简体   繁体   English

R:ifelse语句和seq()函数参数

[英]R: ifelse statement and seq() function parameters

I am trying to define some options for the sequences of my y-axis in a baseR plot loop. 我正在尝试为baseR绘图循环中的y轴序列定义一些选项。 I am trying to achieve this by using an if else statement where I define the axis labels like this 我试图通过使用if else语句来实现这一点,在该语句中我定义了这样的轴标签

if(any(y < 100000 & y > -100000)){plotlabels <- c(-20000000, 20000000, 10000)} else {plotlabels <- c(-20000000, 20000000, 1000)}

then comes the plot loop and after that I am calling an axis() command for the y axis like this 然后是绘图循环,之后我像这样为y轴调用axis()命令

axis(2, at = seq(plotlabels), cex.axis=1, labels=TRUE, tcl=-0.3)

But this will not work correctly as I get a vector by using c() for plotlabels and if I print the result I get the following 但这将无法正常工作,因为我通过使用c()作为plotlabels获得矢量,并且如果我打印结果,则会得到以下结果

seq(plotlabels)
[1] 1 2 3

How could I change the if else statement so that I get the correct sequence parameters according to the if else statement (-20000000, 20000000, 10000 or -20000000, 20000000, 1000)? 如何更改if else语句,以便根据if else语句(-20000000、20000000、10000或-20000000、20000000、1000)获得正确的序列参数?

If you use seq with a vector with at least two elements, the function will return the numbers from one to the vector length. 如果将seq与至少包含两个元素的向量一起使用,则该函数会将数字从1返回到向量长度。

vec <- c(-10, 10, 2)
seq(vec)
# [1] 1 2 3

You can use one of the following approaches to obtain a correct sequence: 您可以使用以下方法之一来获取正确的序列:

seq(vec[1], vec[2], vec[3])
# [1] -10  -8  -6  -4  -2   0   2   4   6   8  10

do.call(seq, as.list(vec))
# [1] -10  -8  -6  -4  -2   0   2   4   6   8  10

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

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