官术网_书友最值得收藏!

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 horizontal
  • 2: This is always perpendicular to the axis
  • 3: 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, and 4=right)
  • at: This takes a vector of coordinates where tick marks are to be drawn
  • labels: 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.

主站蜘蛛池模板: 曲周县| 哈巴河县| 洪洞县| 琼中| 大姚县| 山东省| 德保县| 三河市| 贺州市| 双峰县| 嘉鱼县| 台东市| 固安县| 湟源县| 京山县| 云浮市| 察隅县| 乐安县| 高邮市| 和政县| 沛县| 定襄县| 育儿| 徐闻县| 扬中市| 社会| 泰顺县| 京山县| 潢川县| 如皋市| 永嘉县| 唐海县| 贞丰县| 孟津县| 册亨县| 红安县| 丰台区| 汉源县| 博白县| 奈曼旗| 隆德县|