读入 R 包 ggplot2 中的数据集 mpg, 统计该数据集中各汽车厂商 (manufacturer) 生产汽车的数量, 并用条 形图进行展示; • 对道奇车型中, 对不同气缸数 (cyl) 分别为 4, 5, 6, 8 的车型进行统计, 并用颜色标记出其中驱动方式 (drv) 不同的数据; • 分别比较 1999 与 2008 年中各类型 (class) 车的占比, 并统计 1999 年 与 2008 年排量在 2 到 4 L 中各车型 (class) 的数量.
时间: 2023-06-09 10:03:58 浏览: 89
好的,您的第一个问题是如何统计R包ggplot2中数据集mpg中各汽车厂商(manufacturer)生产汽车的数量,并用条形图进行展示。
您可以按照以下步骤进行操作:
1. 导入ggplot2库和mpg数据。
```r
library(ggplot2)
data(mpg)
```
2. 对manufacturer进行分组,统计数量并按照数量进行排序。
```r
mpg_manufacturer_count <- mpg %>% group_by(manufacturer) %>% count()
mpg_manufacturer_count <- mpg_manufacturer_count[order(mpg_manufacturer_count$n, decreasing = TRUE),]
```
3. 使用ggplot2库中的geom_bar函数将结果绘制成条形图。
```r
ggplot(mpg_manufacturer_count, aes(x = reorder(manufacturer, n), y = n)) +
geom_bar(stat = "identity", fill = "#0072B2") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle("汽车厂商生产汽车数量统计") +
xlab("汽车厂商") +
ylab("汽车数量")
```
效果如下图所示:
![mpg_manufacturer_count](https://img-blog.csdnimg.cn/20211207174428492.png)
您的第二个问题是如何对道奇车型中,对不同气缸数(cyl)分别为4、5、6、8的车型进行统计,并用颜色标记出其中驱动方式(drv)不同的数据。
您可以按照以下步骤进行操作:
1. 导入dplyr库和mpg数据。
```r
library(dplyr)
data(mpg)
```
2. 定义颜色映射表。
```r
cyl_dodge_colors <- c("gray30", "gray50", "gray70", "gray90")
drv_dodge_colors <- c("#0072B2", "#E69F00", "#D55E00")
names(cyl_dodge_colors) <- c(4, 5, 6, 8)
names(drv_dodge_colors) <- c("r", "f", "4")
```
3. 对不同气缸数和驱动方式的车型进行分组统计。
```r
mpg_dodge_count <- mpg %>%
filter(manufacturer == "dodge") %>%
group_by(cyl, drv) %>%
count()
```
4. 使用ggplot2库中的geom_col函数将结果绘制成分组柱状图,用颜色区分不同气缸数并用fill参数设置驱动方式的颜色。
```r
ggplot(mpg_dodge_count, aes(x = as.factor(cyl), y = n, fill = drv)) +
geom_col(position = "dodge") +
scale_fill_manual(values = drv_dodge_colors) +
scale_x_discrete(limits = c("4", "5", "6", "8")) +
scale_y_continuous(limits = c(0, 20), breaks = seq(0, 20, by = 5)) +
ggtitle("道奇车型中不同气缸数和驱动方式的车型统计") +
xlab("气缸数") +
ylab("车型数量") +
guides(fill = guide_legend(title = "驱动方式"))
```
效果如下图所示:
![mpg_dodge_count](https://img-blog.csdnimg.cn/20211207174553497.png)
您的第三个问题是如何分别比较1999与2008年中各类型(class)车的占比,并统计1999年与2008年排量在2到4L中各车型(class)的数量。
您可以按照以下步骤进行操作:
1. 导入dplyr库和mpg数据。
```r
library(dplyr)
data(mpg)
```
2. 对1999年与2008年分别进行数据分组,统计各类型(class)车的数量,并计算占比。
```r
mpg_1999_class_count <- mpg %>% filter(year == 1999) %>% group_by(class) %>% count()
mpg_1999_class_count$percent <- round(mpg_1999_class_count$n / sum(mpg_1999_class_count$n) * 100, 2)
mpg_2008_class_count <- mpg %>% filter(year == 2008) %>% group_by(class) %>% count()
mpg_2008_class_count$percent <- round(mpg_2008_class_count$n / sum(mpg_2008_class_count$n) * 100, 2)
```
3. 使用ggplot2库中的geom_bar函数将结果绘制成堆叠条形图。
```r
mpg_class_count <- rbind(mpg_1999_class_count, mpg_2008_class_count)
ggplot(mpg_class_count, aes(x = class, y = percent, fill = factor(year))) +
geom_bar(stat = "identity") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle("1999年与2008年各类型车的占比") +
xlab("车型类型") +
ylab("百分比") +
guides(fill = guide_legend(title = "年份"))
```
效果如下图所示:
![mpg_class_count_percent](https://img-blog.csdnimg.cn/20211207174731728.png)
4. 分别统计1999年与2008年排量在2到4L中各车型统计量,并使用ggplot2库中的geom_col函数将结果绘制成分组柱状图。
```r
mpg_1999_2008_within_2_4L <- mpg %>%
filter(displ >= 2 & displ <= 4) %>%
group_by(year, class) %>%
count()
ggplot(mpg_1999_2008_within_2_4L, aes(x = class, y = n, fill = factor(year))) +
geom_col(position = "dodge") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ggtitle("1999年与2008年排量在2到4L中各车型统计") +
xlab("车型类型") +
ylab("数量") +
guides(fill = guide_legend(title = "年份"))
```
效果如下图所示:
![mpg_class_count_within_2_4L](https://img-blog.csdnimg.cn/20211207174819156.png)
以上就是您提出的三个问题的解答过程,希望解答能够对您有所帮助!