R绘图:线条样式、坐标轴设置与辅助函数应用
立即解锁
发布时间: 2025-09-02 01:34:06 阅读量: 8 订阅数: 19 AIGC 

### R 绘图:线条样式、坐标轴设置与辅助函数应用
#### 1. 线条样式设置
在 R 中,`plot` 函数生成的线条可以进行样式设置,主要涉及四个参数:`lwd`、`lend`、`ljoin` 和 `lmitre`。
- **`lwd`(线条宽度)**:该参数可设置为任意正数,默认值为 1。不过,`lwd` 的图形解释依赖于绘图设备,不同设备上的效果可能不一致。
- **`lend`(线条末端样式)**:可以取整数值 0、1、2 或字符值 “round”、“butt”、“square”,默认值为 0 或 “round”。
以下是设置 `lwd` 和 `lend` 的代码示例:
```R
plot(
pop75.ordered, pop15.ordered, type="b",
main="default plot",
xlim=c( 2, 3 )
)
plot(
pop75.ordered, pop15.ordered, type="b",
main="lwd=2, lend=0",
lwd=2,
xlim=c( 2, 3 )
)
plot(
pop75.ordered, pop15.ordered, type="b",
main="lwd=3, lend=1",
lwd=3,
lend=1,
xlim=c( 2, 3 )
)
plot(
pop75.ordered, pop15.ordered, type="b",
main="lwd=4, lend=2",
lwd=4,
lend=2,
xlim=c( 2, 3 )
)
```
- **`ljoin`(线条连接样式)**:有三种可能取值,既可以是数字也可以是字符串:0 或 “round”、1 或 “mitre”、2 或 “bevel”。如果选择 1 或 “mitre”,则可以设置 `lmitre` 参数来控制斜接何时变为斜切。`ljoin` 的默认值为 0 或 “round”,`lmitre` 的默认值为 10,且 `lmitre` 必须大于或等于 1。
以下是设置 `ljoin` 和 `lmitre` 的代码示例:
```R
plot(
pop75.ordered, pop15.ordered, type="l", lwd=2,
main="ljoin = \"round\" (the default)"
)
plot(
pop75.ordered, pop15.ordered, type="l", lwd=2,
main="ljoin = \"bevel\"",
ljoin="bevel"
)
plot(
pop75.ordered, pop15.ordered, type="l", lwd=2,
main="ljoin = \"mitre\", lmitre=2",
ljoin=1,
lmitre=2
)
plot(
pop75.ordered, pop15.ordered, type="l", lwd=2,
main="ljoin = \"mitre\", lmitre=100",
ljoin=1,
lmitre=100
)
```
#### 2. 坐标轴设置
在 `plot` 函数调用中,可以通过多种方式更改坐标轴刻度和刻度标签。
- **`las`(坐标轴刻度标签方向)**:取值为 0(与坐标轴平行)、1(始终水平)、2(与坐标轴垂直)、3(始终垂直),默认值为 0。
以下是设置 `las` 的代码示例:
```R
plot(
pop75.ordered, pop15.ordered,
las=0,
main="las = 0"
)
plot(
pop75.ordered, pop15.ordered,
las=1,
main="las = 1"
)
plot(
pop75.ordered, pop15.ordered,
las=2,
main="las = 2"
)
plot(
pop75.ordered, pop15.ordered,
las=3,
main="las = 3"
)
```
- **`xaxs` 和 `yaxs`(坐标轴边缘空间)**:取值为 “r”(默认,使用 4% 的空间)和 “i”(不使用空间)。
以下是设置 `xaxs` 和 `yaxs` 的代码示例:
```R
plot(
pop75.ordered, pop15.ordered,
main="xaxs = \"r\", yaxs = \"r\"",
xaxs="r",
yaxs="r"
)
plot(
pop75.ordered, pop15.ordered,
main="xaxs = \"i\", yaxs = \"i\"",
xaxs="i",
yaxs="i"
)
```
- **`axes`、`xaxt` 和 `yaxt`(坐标轴和刻度注释)**:`axes` 是一个长度为 1 的逻辑参数,默认值为 `TRUE`,表示包含坐标轴;`xaxt` 和 `yaxt` 用于单独控制 x 轴和 y 轴,设置为 “s”、“l”、“t” 可绘制标准轴,设置为 “n” 可抑制坐标轴刻度和刻度标签注释。
以下是设置 `xaxt` 和 `yaxt` 的代码示例:
```R
plot(
pop75.ordered, pop15.ordered,
main="xaxt = \"n\", yaxt = \"s\"",
xaxt="n",
yaxt="s"
)
```
- **`lab`、`xaxp` 和 `yaxp`(刻度间距)**:`lab` 是一个三元整数向量,第一个元素设置 x 轴的刻度数,第二个元素设置 y 轴的刻度数,第三个元素未使用,默认值为 `c( 5, 5, 7 )`。`xaxp` 和 `yaxp` 可分别设置每个轴的间距,它们是三元数值向量,第一个元素是轴的下限,第二个元素是轴的上限,第三个数字是刻度间距的间隔数。
以下是设置 `lab` 的代码示例:
```R
plot(
pop75.ordered, pop15.ordered,
main="lab = c( 10, 7, 7 )",
lab=c( 10, 7, 7 )
)
plot(
pop75.ordered, pop15.ordered,
main="lab = c( 10, 7, 7 )\nxlim=c( 0.5, 5 ), ylim=c( 20, 50 )",
lab=c( 10, 7, 7 ),
xlim=c( 0.5, 5),
ylim=c( 20, 50 )
)
```
以下是设置 `xaxp` 和 `yaxp` 的代码示例:
```R
plot(
pop75.ordered, pop15.or
```
0
0
复制全文
相关推荐



