How to have static axis values ​​when plotting in R?

I am using the following code:

x <-sample(1:100, 10); y <-sample(1:100, 10); plot(x,y); 

but I'm trying to create a graph with static values ​​along the x, y axis. The problem is that sometimes, for example, the x-axis represents a range of values ​​from 0 to 80 on the graph, and others from 20 to 100, depending on random values ​​inside x.

I would like both x, y to have in all cases a range of values ​​from 0 to 100, regardless of the values ​​inside x and y.

Is there any way to achieve this?

Thanks in advance!

+4
source share
3 answers

Depending on what exactly you mean by “a range of values ​​from 1 to 100,” one of the following should do the trick.

In the first, plot by default extends the axis limits by 4% at both ends

In the second case, xaxs="i" and yaxs="i" are used to suppress this behavior. (For more details see ?par .)

 plot(x,y, xlim=c(0,100), ylim=c(0,100)) 

enter image description here

 plot(x,y, xlim=c(0,100), ylim=c(0,100), xaxs="i", yaxs="i") 

enter image description here

+5
source

There are two functions that you can use, xlim() and ylim() inside plot() , which you can use to manually set the axes.

+1
source

First you need to find out the boundaries - you can select some (for example, 0, 100) or find out the real boundaries using the range function. Then you will use the xlim and ylim . This is an example of finding real boundaries using range :

 x1 <-sample(1:100, 10); y1 <-sample(1:100, 10); x2 <-sample(1:100, 10); y2 <-sample(1:100, 10); par(mfrow=c(1, 2)) range_x <- range(x1, x2); // find the range that spans all the data, for x range_y <- range(y1, y2); // and for y plot(x1, y1, xlim = range_x, ylim = range_y); plot(x2, y2, xlim = range_x, ylim = range_y); 
+1
source

Source: https://habr.com/ru/post/1384381/


All Articles