R 语言绘制线条
线条图形
线条图形是一条连接图表中所有点的线。要创建直线,请使用 plot() 函数并添加值为 "l" 的 type 类型参数:
实例
bitmap(file="out.png")# Plot numbers from 1 to 10 and draw a lineplot(1:10, type="l")
结果如下:

线条颜色
默认情况下,线条颜色为黑色。要更改颜色,请使用 col 参数:
实例
bitmap(file="out.png")# Plot numbers from 1 to 10 and draw a blue lineplot(1:10, type="l", col="blue")
结果如下:

线条宽度
要更改线的宽度,请使用 lwd 参数(默认值为 1,0.5 表示小 50%,2 表示大 100%):
实例
bitmap(file="out.png")# Plot numbers from 1 to 10 and draw a thick lineplot(1:10, type="l", lwd=2)
结果如下:

线条样式
默认情况下,该线为实线。使用值为 0 到 6 的 lty 参数指定行线条样式。
例如,lty=3 将显示虚线而不是实线:
实例
bitmap(file="out.png")# Plot numbers from 1 to 10 and draw a thick dotted lineplot(1:10, type="l", lwd=5, lty=3)
结果如下:

lty 的可用参数值:
- 0 将删除该行
- 1 显示一条实线
- 2 显示一条虚线
- 3 显示一条虚线
- 4 显示一条 "点虚线"
- 5 显示一条 "长虚线"
- 6 显示一条 "双虚线"
多个线条
要在图形中显示多条直线,请将 plot() 函数与 lines() 函数一起使用:
实例
line1 <- c(1,2,3,4,5,10)line2 <- c(2,5,7,8,9,10)plot(line1, type = "l", col = "blue")lines(line2, type="l", col = "red")
结果如下:
