- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 360字
- 2021-08-05 17:30:29
Adjusting x and y axes' limits
In this recipe, we will learn how to adjust the x and y limits of plots, which is useful in adjusting a graph to suit your presentation needs and adding additional data to the same plot.
How to do it...
We will modify our first scatter plot example to demonstrate how to adjust axes limits:
plot(cars$dist~cars$speed, xlim=c(0,30), ylim=c(0,150))

How it works...
In our original scatter plot in the first recipe of this chapter, the x axis limits were set to just below 5 up to 25 and the y axis limits were set from 0 to 120. In this example, we set the x axis limits from 0 to 30 and the y axis limits to 0 to 150 using the xlim
and ylim
arguments, respectively.
Both xlim
and ylim
take a vector of length 2
as valid values in the c(minimum,maximum)
form, that is, xlim=c(0,30)
means set the x axis minimum limit to 0 and maximum limit to 30.
There's more...
You might have noticed that even after setting the x and y limit values, there is a gap left at either edge. The two axes' zeroes don't coincide. This is because R automatically adds some additional space at both edges of the axes so that if there any data points at the extremes, they are not cut off by the axes. If you wish to set the axes limits to exact values, in addition to specifying xlim
and ylim
, you must also set the xaxs
and yaxs
arguments to "i"
:
plot(cars$dist~cars$speed, xlim=c(0,30), ylim=c(0,150), xaxs="i", yaxs="i")

Sometimes, we might wish to reverse a data axis, say, to plot the data in the descending order along one axis. All we have to do is swap the minimum and maximum values in the vector argument supplied as xlim
or ylim
. So, if we want the x axis speed values in the preceding graph in the descending order, we need to set xlim
to c(30,0)
:
plot(cars$dist~cars$speed, xlim=c(30,0), ylim=c(0,150), xaxs="i", yaxs="i")

See also
There are a few more recipes on adjusting the axes tick marks and labels in Chapter 3, Beyond the Basics – Adjusting Key Parameters.
- 筆記本電腦使用、維護(hù)與故障排除實(shí)戰(zhàn)
- Istio入門與實(shí)戰(zhàn)
- 顯卡維修知識精解
- Instant uTorrent
- SDL Game Development
- Mastering Delphi Programming:A Complete Reference Guide
- 硬件產(chǎn)品經(jīng)理手冊:手把手構(gòu)建智能硬件產(chǎn)品
- CC2530單片機(jī)技術(shù)與應(yīng)用
- Spring Cloud微服務(wù)架構(gòu)實(shí)戰(zhàn)
- 固態(tài)存儲:原理、架構(gòu)與數(shù)據(jù)安全
- Source SDK Game Development Essentials
- 單片機(jī)技術(shù)及應(yīng)用
- 筆記本電腦芯片級維修從入門到精通(圖解版)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)案例教程(Windows 7+Office 2010)
- 多媒體應(yīng)用技術(shù)(第2版)