- R Graphs Cookbook Second Edition
- Jaynal Abedin Hrishi V. Mittal
- 391字
- 2021-08-05 17:30:33
Choosing box styles
The styles of various boxes drawn in a graph such as the one around the plotting region and the legend can be adjusted in a similar way to the line styles we saw in the last recipe.
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...
Let's say we want to create an L-shaped box around a graph such that the default top and right borders are not drawn. We can do so using the bty
argument in the par()
command:
par(bty="l") plot(rnorm(100))

How it works...
The bty
argument stands for the box type and takes single characters in inverted commas as values. The resulting box resembles the corresponding uppercase letter. For example, the default value is o
, thus creating a box with all four edges. Other possible values are l
, 7
, c
, u
, and ]
. If we do not wish to draw a box at all, we can set bty
to n
.
Note
Note that setting bty
to n
doesn't suppress the drawing of axes. If we wish to suppress these too, then we would also have to set xaxt
and yaxt
to n
. Alternatively, we can simply set the axes
argument to FALSE
in the plot()
function call.
There's more...
Box styles can be controlled in a finer way using the box()
command. In addition to the lty
and lwd
arguments, we can also specify where the box should be drawn using the which
parameter, which can take values of plot
, figure
, inner
, and outer
.
Let's say we want to draw a graph with an L-shaped box for the plot area and a full box around the figure including the axis annotations and titles. Then, we can use the following code:
par(oma=c(1,1,1,1)) plot(rnorm(100),bty="l") box(which="figure")

Note that we had to first set the outer margins by setting the oma
argument with the par()
function. You will learn more about this argument later in this chapter. If we did not set the outer margins, the box around the figure would be right at the edge of the plot and get cut off because the default margins are set to zero.
- Arduino入門基礎教程
- 24小時學會電腦組裝與維護
- 深入理解Spring Cloud與實戰
- Artificial Intelligence Business:How you can profit from AI
- VCD、DVD原理與維修
- 微服務分布式架構基礎與實戰:基于Spring Boot + Spring Cloud
- R Deep Learning Essentials
- 筆記本電腦使用、維護與故障排除從入門到精通(第5版)
- 基于網絡化教學的項目化單片機應用技術
- 單片機項目設計教程
- Angular 6 by Example
- USB應用分析精粹:從設備硬件、固件到主機端程序設計
- The Machine Learning Workshop
- 施耐德M241/251可編程序控制器應用技術
- 計算機組裝與維護立體化教程(微課版)