- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 457字
- 2021-08-05 17:30:33
Adjusting axis annotations and tick marks
The default axis settings are often not adequate to deal with all kinds of data. For example, we might wish to change the number of tick marks along an axis or change the orientation of the annotations if they are too long in order to fit them horizontally. In this recipe, we will cover some settings that can be used to customize axes as per our requirements.
Getting ready
All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.
How to do it...
We can set the xaxp
and yaxp
arguments with the par()
command in order to specify coordinates of the extreme tick marks and the number of intervals between tick marks in the c(min,max,n)
form:
plot(rnorm(100),xaxp=c(0,100,10))

How it works...
When xaxp
or yaxp
is not specified, R automatically calculates the number of tick marks and their values. By default, R extends the axis limits by adding 4 percent at each end and then draws an axis that fits within the extended range. This means that even if we set the axis limits using xlim
or ylim
, the graph corners don't exactly correspond with these values. To make sure they do, we need to change the axis style using the xaxs
argument, which takes one of the two possible values: r
(the regular or default) and i
(internal). We need to set xaxs
to i
.
A vector of the c(x1,
x2,
n)
form gives the coordinates of the extreme tick marks and the number of intervals between tick marks.
There's more...
To change the orientation of axis value annotations, we need to set the las
argument of the par()
command. This takes one of the four possible numeric values:
0
: This is always parallel to the axis (the default)1
: This is always horizontal2
: This is always perpendicular to the axis3
: This is always vertical
We can also use the axis()
command to create a custom axis by specifying a number of arguments. The basic arguments are as follows:
side
: This takes numeric values (1
=below,2
=left,3
=above, and4
=right)at
: This takes a vector of coordinates where tick marks are to be drawnlabels
: This takes a vector of tick mark annotations
We can set the line width for the axis lines and the tick marks separately by passing the lwd
and lwd.ticks
arguments, respectively. Similarly, colors can be set using the col
and col.ticks
arguments.
See also
We will come across various examples of custom axes in the following chapters as we explore more advanced recipes.
- 基于Proteus和Keil的C51程序設(shè)計(jì)項(xiàng)目教程(第2版):理論、仿真、實(shí)踐相融合
- 深入淺出SSD:固態(tài)存儲(chǔ)核心技術(shù)、原理與實(shí)戰(zhàn)(第2版)
- Mastering Manga Studio 5
- AMD FPGA設(shè)計(jì)優(yōu)化寶典:面向Vivado/SystemVerilog
- 電腦軟硬件維修從入門到精通
- OUYA Game Development by Example
- Spring Cloud微服務(wù)架構(gòu)實(shí)戰(zhàn)
- 基于PROTEUS的電路設(shè)計(jì)、仿真與制板
- Python Machine Learning Blueprints
- Hands-On Deep Learning for Images with TensorFlow
- 電腦橫機(jī)使用與維修
- Raspberry Pi Home Automation with Arduino
- 嵌入式系統(tǒng)設(shè)計(jì)大學(xué)教程(第2版)
- 從企業(yè)級(jí)開(kāi)發(fā)到云原生微服務(wù):Spring Boot實(shí)戰(zhàn)
- 電腦軟硬件維修寶典