R语言--图表(四)

1. 条形图
  • 语法
    在R语言中创建条形图的基本语法是 -
barplot(H, xlab, ylab, main, names.arg, col)

以下是所使用的参数的描述 -
-- H是包含在条形图中使用的数值的向量或矩阵。
-- xlab是x轴的标签。
-- ylab是y轴的标签。
-- main是条形图的标题。
-- names.arg是在每个条下出现的名称的向量。
-- col用于向图中的条形提供颜色。

  • 创建普通条形图
# 为图表创建数据
H <- c(7, 12, 28, 3, 41)
# 设置文件名
png(file = "barchart.png")
# 绘制图表
barplot(H)
# 保存图片
dev.off()

效果:


3110861-23415f67c9b19a04.png
barchart.png
  • 条形图标签,标题和颜色
# 条形图标签,标题和颜色
# 为图表创建数据
H <- c(7, 12, 28, 3, 41)
M <- c("Mar", "Apr", "May", "Jun", "Jul")
# 设置文件名
png(file = "barchart_months_revenue.png")
# 绘制图表
barplot(H, names.arg = M, xlab = "Month", ylab = "Revenue", col = "blue", main = "Revenue chart", border = "red")
# 保存文件
dev.off()

效果:


3110861-e55bdf2500d5656b.png
barchart_months_revenue.png
  • 组合条形图和堆积条形图
# 组合条形图和堆积条形图
# 创建输入向量
colors <- c("green", "orange", "brown")
months <- c("Mar", "Apr", "May", "Jun", "Jul")
regions <- c("East", "West", "North")
# 创建数据矩阵
values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11), nrow = 3, ncol = 5, byrow = TRUE)
# 设置文件名
png(file = "barchart_stacked.png")
# 创建条形图
barplot(values, main = "total revenue", names.arg = months, xlab = "month", ylab = "revenue", col = colors)
# 添加图例
legend("topleft", regions, cex = 1.3, fill = colors)
# 保存文件
dev.off()

效果:


3110861-9b3f8914f63252e2.png
barchart_stacked.png
2. 箱线图
  • 语法
    在R语言中创建箱线图的基本语法是 -
    boxplot(x, data, notch, varwidth, names, main)
    以下是所使用的参数的描述 -
    -- x是向量或公式。
    -- 数据是数据帧。
    -- notch是逻辑值。 设置为TRUE以绘制凹口。
    -- varwidth是一个逻辑值。 设置为true以绘制与样本大小成比例的框的宽度。
    -- names是将打印在每个箱线图下的组标签。
    -- main用于给图表标题。

  • 数据集

# 打印数据集
input <- mtcars[, c('mpg', 'cyl')]
print(input)

打印结果:


3110861-c47b32d5f4a23026.png
图1.png
  • 创建箱线图
# 设置文件名
png(file = "boxplot.png")
# 绘制图表
boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders", ylab = "Miles Per Gallon", main = "Mileage Data")
# 保存文件
dev.off()

效果:


3110861-138096822188e841.png
boxplot.png
  • 带槽的箱线图
# 设置文件名
png(file = "boxplot_with_notch.png")
# 绘制图表
boxplot(mpg ~ cyl, data = mtcars,
    xlab = "Number of Cylinders",
    ylab = "Miles Per Gallon", 
    main = "Mileage Data",
    notch = TRUE,
    varwidth = TRUE,
    col = c("green", "yellow", "purple"),
    names = c("Hign", "Medium", "Low")
)
# 保存文件
dev.off()

效果:


3110861-298e35999ae69794.png
boxplot_with_notch.png
3. 直方图
  • 语法
    使用R语言创建直方图的基本语法是 -
    hist(v,main,xlab,xlim,ylim,breaks,col,border)
    以下是所使用的参数的描述 -
    -- v是包含直方图中使用的数值的向量。
    -- main表示图表的标题。
    -- col用于设置条的颜色。
    -- border用于设置每个条的边框颜色。
    -- xlab用于给出x轴的描述。
    -- xlim用于指定x轴上的值的范围。
    -- ylim用于指定y轴上的值的范围。
    -- breaks用于提及每个条的宽度。

  • 创建直方图

# 创建直方图
# 创建向量
v <- c(9, 13, 21, 8, 36, 22, 12, 41, 31, 33, 19)
# 设置文件名
png(file = "histogram.png")
# 创建直方图
hist(v, xlab = "Weight", col = "yellow", border = "blue")
# 保存文件
dev.off()

效果:


3110861-06b45fc009a5c8c6.png
histogram.png
  • X和Y值的范围
# X和Y值的范围
# 创建向量
v <- c(9, 13, 21, 8, 36, 22, 12, 41, 31, 33, 19)
# 设置文件名
png(file = "histogram_lim_breaks.png")
# 创建直方图
hist(v, xlab = "Weight", col = "green", border = "red", xlim = c(0, 50), ylim = c(0, 5), breaks = 4)
# 保存文件
dev.off()

效果:


3110861-f7ff99499f659899.png
histogram_lim_breaks.png
4. 折线图
  • 语法
    在R语言中创建折线图的基本语法是 -
    plot(v,type,col,xlab,ylab)
    以下是所使用的参数的描述 -
    -- v是包含数值的向量。
    -- 类型采用值“p”仅绘制点,“l”仅绘制线和“o”绘制点和线。
    -- xlab是x轴的标签。
    -- ylab是y轴的标签。
    -- main是图表的标题。
    -- col用于给点和线的颜色。

  • 创建折线图

# 创建折线图
# 创建图表数据
v <- c(7, 12, 28, 3, 41)
# 设置文件名
png(file = "line_chart.png")
# 绘制折线
plot(v, type = "o")
# 保存文件
dev.off()

效果:


3110861-994b161342364968.png
line_chart.png
  • 折线图标题,颜色和标签
# 创建图表数据
v <- c(7, 12, 28, 3, 41)
# 设置文件名
png(file = "line_chart_label_colored.png")
# 绘制折线
plot(v, type = "o", col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
# 保存文件
dev.off()

效果:


3110861-78037e2c1629648c.png
line_chart_label_colored.png
  • 多线型折线图
# 使用lines()函数,可以在同一个图表上绘制多条线
# 创建图表数据
v <- c(7, 12, 28, 3, 41)
t <- c(14, 7, 6, 19, 3)
# 设置文件名
png(file = "line_chart_2_lines.png")
# 绘制表
plot(v, type = "o", col = "red", xlab = "Month", ylab = "Rain fall", main = "Rain fall chart")
lines(t, type = "o", col = "blue")
# 保存文件
dev.off()

效果:


3110861-dc09aa6d475f02fc.png
line_chart_2_lines.png
5. 散点图
  • 语法
    在R语言中创建散点图的基本语法是 -
plot(x, y, main, xlab, ylab, xlim, ylim, axes)

以下是所使用的参数的描述 -
-- x是其值为水平坐标的数据集。
-- y是其值是垂直坐标的数据集。
-- main要是图形的图块。
-- xlab是水平轴上的标签。
-- ylab是垂直轴上的标签。
-- xlim是用于绘图的x的值的极限。
-- ylim是用于绘图的y的值的极限。
-- axes指示是否应在绘图上绘制两个轴。

  • 数据集
# 数据集
input <- mtcars[, c('wt', 'mpg')]
print(input)

打印结果:


3110861-f1838ccdc1748477.png
图2.png
  • 创建散点图
# 数据
input <- mtcars[, c('wt', 'mpg')]
# 设置文件名
png(file = 'scatterplot.png')
# 创建表
plot(x = input$wt, y = input$mpg,
    xlab = "Weight",
    ylab = "Milage",
    xlim = c(2.5, 5),
    ylim = c(15, 30),
    main = "Weight vs Milage"
)
# 保存文件
dev.off()

效果图:


3110861-42adc64612674754.png
scatterplot.png
  • 散点图矩阵
    在R中创建散点图矩阵的基本语法是 -
pairs(formula, data)

以下是所使用的参数的描述 -
-- formula表示成对使用的一系列变量。
-- data表示将从其获取变量的数据集。
示例:

# 设置文件名
png(file = "scatterplot_matrices.png")
# 绘制
pairs(~ wt + mpg + disp + cyl, data = mtcars, main = "Scatterplot Matrix")
# 保存文件
dev.off()

效果:


3110861-1b863c84028b19c1.png
scatterplot_matrices.png
6. 饼状图
  • 语法
    使用R语言创建饼图的基本语法是 -
pie(x, labels, radius, main, col, clockwise)

以下是所使用的参数的描述 -
-- x是包含饼图中使用的数值的向量。
-- labels用于给出切片的描述。
-- radius表示饼图圆的半径(值-1和+1之间)。
-- main表示图表的标题。
-- col表示调色板。
-- clockwise是指示片段是顺时针还是逆时针绘制的逻辑值。

  • 创建饼状图
# 饼状图
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
# 设置文件名
png(file = "city.png")
# 绘制饼图
pie(x, labels)
# 保存文件
dev.off()

效果:


3110861-e58ea1a384eaebd6.png
city.png
  • 饼图标题和颜色
# 数据
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
# 设置文件名
png(file = "city_title_colors.png")
# 绘制
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))
# 保存文件
dev.off()

效果:


3110861-b63e8e62e1e3bdba.png
city_title_colors.png
  • 切片百分比和图表图例
# 数据
x <- c(21, 62, 10, 53)
labels <- c("London", "New York", "Singapore", "Mumbai")
# 计算百分比
piepercent <- round(100 * x / sum(x), 1)
# 设置文件名
png(file = "city_percentage_legends.png")
# 绘制图表
pie(x, labels = piepercent, main = "City pie chart", col = rainbow(length(x)))
# 图例
legend("topright", c("London", "New York", "Singapore", "Mumbai"), cex = 0.8, fill = rainbow(length(x)))
# 保存文件
dev.off()

效果:


3110861-7b26b9dd83f48306.png
city_percentage_legends.png
  • 3D饼图
# 安装包
install.packages("plotrix", repos="https://cran.cnr.berkeley.edu/")
# 引入库
library(plotrix)
# 创建数据
x <- c(21, 62, 10, 53)
lbl <- c("London", "New York", "Singapore", "Mumbai")
# 设置文件名
png(file = "3d_pie_chart.png")
# 绘制图表
pie3D(x, labels = lbl, explode = 0.1, main = "Pie chart Of Countries")
# 保存文件
dev.off()

效果:


3110861-f22a122a8165596d.png
3d_pie_chart.png
代码下载
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值