指标 2021年 2020年 2019年 2018年 2017年 2016年 2015年 2014年 2013年 2012年 旅客运输平均运距(公里) 237.98 199.18 200.8 190.76 177.5 164.5 154.68 140.96 129.87 87.76 铁路旅客运输平均运距(公里) 366.34 375.14 401.82 419.16 436.38 447.02 471.85 487.8 503.12 518.25 公路旅客运输平均运距(公里) 71.31 67.32 68.07 67.88 67.03 66.3 66.35 63.34 60.7 51.92 水运旅客运输平均运距(公里) 20.27 22.01 29.42 28.44 27.44 26.56 27 28.27 29.03 30.09 民用航空旅客运输平均运距(公里) 1482.14 1510.68 1773.71 1751.13 1724.75 1716.97 1669.62 1616.08 1598.11 1573.69 用R语言对该csv文件进行数据分析,并绘图,要求不低于五个绘图,该csv文件的名字是sos.csv
时间: 2024-03-25 19:41:59 浏览: 15
好的,以下是针对该CSV文件的R语言数据分析和绘图代码:
首先,读取CSV文件:
```R
data <- read.csv("sos.csv", header = TRUE, stringsAsFactors = FALSE)
```
接着,我们可以使用`summary()`函数和`sapply()`函数来查看数据的基本情况和数据类型:
```R
summary(data)
sapply(data, class)
```
然后,我们可以通过使用`ggplot2`库来绘制以下五个图表:
1. 旅客运输平均运距与年份的折线图
```R
library(ggplot2)
ggplot(data, aes(x = rownames(data), y = 旅客运输平均运距.公里.)) +
geom_line() +
labs(title = "旅客运输平均运距与年份的折线图", x = "年份", y = "旅客运输平均运距(公里)")
```
2. 各种交通方式的平均运距折线图
```R
ggplot(data, aes(x = rownames(data), y = 旅客运输平均运距.公里., group = 1)) +
geom_line(aes(color = "总体平均运距")) +
geom_line(aes(y = 铁路旅客运输平均运距.公里., color = "铁路")) +
geom_line(aes(y = 公路旅客运输平均运距.公里., color = "公路")) +
geom_line(aes(y = 水运旅客运输平均运距.公里., color = "水运")) +
geom_line(aes(y = 民用航空旅客运输平均运距.公里., color = "民航")) +
labs(title = "各种交通方式的平均运距折线图", x = "年份", y = "旅客运输平均运距(公里)", color = "交通方式") +
scale_color_manual(values = c("总体平均运距" = "black", "铁路" = "red", "公路" = "blue", "水运" = "green", "民航" = "purple"))
```
3. 各种交通方式的平均运距箱线图
```R
data_long <- reshape2::melt(data, id.vars = "指标", variable.name = "年份")
data_long$交通方式 <- gsub("\\..*", "", data_long$指标)
data_long$指标 <- gsub("^.*\\.", "", data_long$指标)
ggplot(data_long, aes(x = 交通方式, y = value, fill = 交通方式)) +
geom_boxplot() +
labs(title = "各种交通方式的平均运距箱线图", x = "交通方式", y = "旅客运输平均运距(公里)", fill = "交通方式")
```
4. 各年份旅客运输平均运距的直方图
```R
ggplot(data_long, aes(x = value)) +
geom_histogram(bins = 10) +
facet_wrap(~年份, ncol = 3) +
labs(title = "各年份旅客运输平均运距的直方图", x = "旅客运输平均运距(公里)", y = "频数")
```
5. 各交通方式旅客运输平均运距的密度图
```R
ggplot(data_long, aes(x = value, color = 交通方式)) +
geom_density() +
labs(title = "各交通方式旅客运输平均运距的密度图", x = "旅客运输平均运距(公里)", y = "密度", color = "交通方式")
```
以上就是基于该CSV文件所做的五个绘图。