Set R-chart xlim only with a lower border

Suppose I created a simple graph as follows:

xvalues <- 100:200 yvalues <- 250:350 plot(xvalues, yvalues) 

enter image description here

However, I would like the x axis to start at 0 and leave the upper bound to the one that R computes. How can I do this?

I know that there is an option for xlim = c (lower bound, upper bound), but I do not know what the upper bound is. In addition, I apparently cannot say that the upper bound is not defined:

 > plot(xvalues, yvalues, xlim=c(0)) Error in plot.window(...) : invalid 'xlim' value 

It would be great if I did not have to calculate the maximum value of the xvalues โ€‹โ€‹vector to get the upper bound, as this seems wasteful for a very large data vector.

+7
r
source share
2 answers

You can use one of two approaches:

Limit calculation

 xlim <- c(0, max(xvalues)) 

xlim can now be represented as an argument to xlim in plot .

 xvalues <- 100:200 yvalues <- 250:350 plot(xvalues, yvalues, xlim=xlim) 

enter image description here

Let par return the limits

This is a little more complicated, but sometimes useful (certainly redundant in your case, but for completeness). You print the data once, you get the borders of the chart area in user coordinates using par("usr") . Now you can use them in your new plot.

 plot(xvalues, yvalues, xaxs="i") xmax <- par("usr")[2] plot(xvalues, yvalues, xlim=c(0,xmax)) 

PS. I used xaxs="i" , so the results will be without small extensions at the ends.

+5
source share

you can simply set max for x using max values:

 xvalues <- 1:99 yvalues <- rep(1,99) plot(xvalues, yvalues, xlim = c(0, max(xvalues)) ) 

enter image description here

+1
source share

All Articles