R语言 教程
- 第1节:R语言 概述
- 第2节:R语言 环境设置
- 第3节:R语言 基本语法
- 第4节:R语言 数据类型
- 第5节:R语言 变量
- 第6节:R语言 运算符
- 第7节:R语言 决策
- 第8节:R语言 包
- 第9节:R语言 循环
- 第10节:R语言 数据重塑
- 第11节:R语言 函数
- 第12节:R语言 字符串
- 第13节:R语言 向量
- 第14节:R语言 列表
- 第15节:R语言 矩阵
- 第16节:R语言 数组
- 第17节:R语言 因子
- 第18节:R语言 数据帧
- 第19节:R语言 条形图
- 第20节:R语言 箱线图
- 第21节:R语言 直方图
- 第22节:R语言 折线图
- 第23节:R语言 散点图
- 第24节:R语言 饼状图
- 第25节:R语言 CSV文件
- 第26节:R语言 Excel文件
- 第27节:R语言 二进制文件
- 第28节:R语言 XML文件
- 第29节:R语言 JSON文件
- 第30节:R语言 Web数据
- 第31节:R语言 数据库
- 第32节:R语言 平均值,中位数和模式
- 第33节:R语言 线性回归
- 第34节:R语言 多重回归
- 第35节:R语言 逻辑回归
- 第36节:R语言 标准分布
- 第37节:R语言 二项分布
- 第38节:R语言 泊松回归
- 第39节:R语言 协方差分析
- 第40节:R语言 时间序列分析
- 第41节:R语言 非线性最小二乘
- 第42节:R语言 决策树
- 第43节:R语言 随机森林算法
- 第44节:R语言 生存分析
- 第45节:R语言 卡方检验
R语言 折线图
折线图是通过在它们之间绘制线段来连接一系列点的图。 这些点在它们的坐标(通常是x坐标)值之一中排序。 折线图通常用于识别数据中的趋势。
R语言中的plot()函数用于创建折线图。
语法
在R语言中创建折线图的基本语法是 -
plot(v,type,col,xlab,ylab)
以下是所使用的参数的描述 -
v是包含数值的向量。
类型采用值“p”仅绘制点,“l”仅绘制线和“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()函数,可以在同一个图表上绘制多条线。在绘制第一行之后,lines()函数可以使用一个额外的向量作为输入来绘制图表中的第二行。
# 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()
当我们执行上面的代码,它产生以下结果 -