发布于 2016-01-02 09:31:35 | 1110 次阅读 | 评论: 0 | 来源: 网络整理

线性图是通过绘制它们之间的线段连接的一系列点的图表。这些点是有序的在其坐标(通常是x坐标)值中的一个。线图通常用于识别趋势的数据。

R中的 plot()函数是用于创建线图。

语法

R中用来创建一个线图基本的语法来是:
plot(v,type,col,xlab,ylab)

以下是所使用的参数的说明:

  • v - 是一个包含数字值的向量。

  • type - 取值“p”只是用来绘制点,“i”只绘制线条,而 “o”画两个点和线。

  • xlab - 是标签为X轴。

  • ylab - 是标签为Y轴。

  • main - 是图表的标题。

  • col - 用于给出点和线的颜色。

示例

使用输入向量和类型参数为“O”来创建一个简单的折线图。 下面的脚本将创建和保存线图在R的当前工作目录。

# Create the data for the chart.
v <- c(7,12,28,3,41)

# Give the chart file a name.
png(file = "line_chart.jpg")

# Plot the bar chart. 
plot(v,type="o")

# Save the file.
dev.off()

当我们上面的代码执行时,它产生以下结果:

线图表标题,颜色和标签

线图的特征可通过使用附加参数进行扩展。我们增添点和线的颜色,给出图表的标题,并添加轴的标签。

示例

# Create the data for the chart.
v <- c(7,12,28,3,41)

# Give the chart file a name.
png(file = "line_chart_label_colored.jpg")

# Plot the bar chart.
plot(v,type="o",col="red",xlab="Month",ylab="Rain fall",main="Rain fall chart")

# Save the file.
dev.off()

当我们上面的代码执行时,它产生以下结果:

线图中的多条直线

多于一行可以通过使用 lines()函数来绘制在同一图表上。

第一线绘制之后,line()函数可以使用一个附加的矢量作为输入来绘制第二条线在图中

# Create the data for the chart.
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)

# Give the chart file a name.
png(file = "line_chart_2_lines.jpg")

# Plot the bar chart.
plot(v,type="o",col="red",xlab="Month",ylab="Rain fall",main="Rain fall chart")

lines(t, type="o", col="blue")

# Save the file.
dev.off()

当我们上面的代码执行时,它产生以下结果:




最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务