用r语言plot函数绘制iris数据集的条形图,饼图,直方图,散点图,线图,箱线图, 每种类型2张图,体现不同参数值带来的变化
时间: 2024-05-22 17:10:28 浏览: 140
以下是对 iris 数据集绘制不同类型图表的代码和图像。
## 导入数据集
data(iris)
iris <- iris
## 条形图
# 按照花的种类(Species)统计花萼长度(Sepal.Length)的平均值
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
labs(title = "Bar Plot of Sepal Length by Species", x = "Species", y = "Sepal Length")
# 按照花的种类(Species)统计花萼宽度(Sepal.Width)的平均值
ggplot(iris, aes(x = Species, y = Sepal.Width, fill = Species)) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
labs(title = "Bar Plot of Sepal Width by Species", x = "Species", y = "Sepal Width")
## 饼图
# 按照花的种类(Species)统计花瓣长度(Petal.Length)的总和
library(plotrix)
pie3D(tapply(iris$Petal.Length, iris$Species, sum), labels = c("setosa", "versicolor", "virginica"),
main = "Pie Chart of Petal Length by Species")
# 按照花的种类(Species)统计花瓣宽度(Petal.Width)的总和
pie3D(tapply(iris$Petal.Width, iris$Species, sum), labels = c("setosa", "versicolor", "virginica"),
main = "Pie Chart of Petal Width by Species")
## 直方图
# 统计花萼长度(Sepal.Length)的分布情况
par(mfrow = c(1, 2))
hist(iris$Sepal.Length[iris$Species == "setosa"], main = "Histogram of Sepal Length for setosa",
xlab = "Sepal Length", ylab = "Count", col = "blue", border = "white")
hist(iris$Sepal.Length[iris$Species == "versicolor"], main = "Histogram of Sepal Length for versicolor",
xlab = "Sepal Length", ylab = "Count", col = "green", border = "white")
# 统计花萼宽度(Sepal.Width)的分布情况
hist(iris$Sepal.Width[iris$Species == "setosa"], main = "Histogram of Sepal Width for setosa",
xlab = "Sepal Width", ylab = "Count", col = "blue", border = "white")
hist(iris$Sepal.Width[iris$Species == "versicolor"], main = "Histogram of Sepal Width for versicolor",
xlab = "Sepal Width", ylab = "Count", col = "green", border = "white")
## 散点图
# 绘制花萼长度(Sepal.Length)和宽度(Sepal.Width)之间的散点图
plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species, pch = 19, main = "Scatter Plot of Sepal Length vs. Width",
xlab = "Sepal Length", ylab = "Sepal Width", xlim = c(4, 8), ylim = c(1.5, 5))
# 绘制花瓣长度(Petal.Length)和宽度(Petal.Width)之间的散点图
plot(iris$Petal.Length, iris$Petal.Width, col = iris$Species, pch = 19, main = "Scatter Plot of Petal Length vs. Width",
xlab = "Petal Length", ylab = "Petal Width", xlim = c(0, 8), ylim = c(0, 3))
## 线图
# 统计花萼长度(Sepal.Length)的变化趋势
library(ggplot2)
ggplot(iris, aes(x = seq(1, nrow(iris)), y = Sepal.Length, group = Species, color = Species)) +
geom_line() +
labs(title = "Line Plot of Sepal Length by Species", x = "Index", y = "Sepal Length")
# 统计花瓣长度(Petal.Length)的变化趋势
ggplot(iris, aes(x = seq(1, nrow(iris)), y = Petal.Length, group = Species, color = Species)) +
geom_line() +
labs(title = "Line Plot of Petal Length by Species", x = "Index", y = "Petal Length")
## 箱线图
# 统计花萼长度(Sepal.Length)的分布情况
par(mfrow = c(1, 2))
boxplot(iris$Sepal.Length ~ iris$Species, main = "Boxplot of Sepal Length by Species", col = "blue")
boxplot(iris$Sepal.Length ~ iris$Species, main = "Boxplot of Sepal Length by Species", col = "green", notch = TRUE)
# 统计花萼宽度(Sepal.Width)的分布情况
boxplot(iris$Sepal.Width ~ iris$Species, main = "Boxplot of Sepal Width by Species", col = "blue")
boxplot(iris$Sepal.Width ~ iris$Species, main = "Boxplot of Sepal Width by Species", col = "green", notch = TRUE)
阅读全文