Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have static axis values when plotting in R?

Tags:

plot

r

I am using the following code:

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

but I am trying to create a plot with static values on x, y axis. The problem is that sometimes x axis for example, is presented with a value range from 0 to 80 on the plot, while others from 20 to 100 depending to the random values inside x.

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

Is there a way to achieve this?

Thanks in advance!

like image 880
dr.doom Avatar asked Nov 17 '25 06:11

dr.doom


2 Answers

Depending on what exactly you mean by "value range from 1 to 100", one of the following should do the trick.

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

In the second one, xaxs="i" and yaxs="i" are used to suppress this behaviour. (See ?par for more details.)

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

like image 183
Josh O'Brien Avatar answered Nov 19 '25 20:11

Josh O'Brien


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

like image 30
Stedy Avatar answered Nov 19 '25 22:11

Stedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!