title: “QIMO”
author: “Ruge”
date: ‘2022-06-23’
output: html_document

R Markdown

第一题

加载包

library(tidyverse)
## Warning: 程辑包'tidyverse'是用R版本4.1.3 来建造的
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

数据预处理,对class进行聚合。

best_in_class <- mpg %>%
group_by(class) %>%
filter(row_number(desc(hwy)) == 1)

绘图

ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(color = class)) +
geom_label(
aes(label = model),
data = best_in_class,
nudge_y = 2,
alpha = 0.5
)

第二题

加载包

library(tidyverse)

数据预处理

smaller <- diamonds %>%
filter(carat < 3)

画图

ggplot(data = smaller, mapping = aes(x = carat, y = price)) +
geom_boxplot(mapping = aes(group = cut_number(carat, 20)))

画图

ggplot(data = smaller, mapping = aes(x = carat, y = price)) +
geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)))