版权声明:本套课程材料开源,使用和分享必须遵守「创作共用许可协议 CC BY-NC-SA」(来源引用-非商业用途使用-以相同方式共享)。
## 数据准备
data = airquality
data$Month = as.factor(data$Month)
data$Temp.C = (data$Temp - 32) / 1.8 # 摄氏度 = (华氏度 - 32) / 1.8
str(data)'data.frame': 153 obs. of 7 variables:
$ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
$ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
$ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
$ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
$ Month : Factor w/ 5 levels "5","6","7","8",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Day : int 1 2 3 4 5 6 7 8 9 10 ...
$ Temp.C : num 19.4 22.2 23.3 16.7 13.3 ...
ggplot(data, aes(x=Wind, y=Temp.C, color=Month)) +
geom_point(show.legend=FALSE) +
facet_wrap(~ Month, nrow=1)ggplot(data, aes(x=Wind, y=Temp.C, color=Month)) +
geom_point(show.legend=FALSE) +
geom_smooth(method="lm", se=FALSE, show.legend=FALSE) +
facet_wrap(~ Month, nrow=1)`geom_smooth()` using formula = 'y ~ x'
## 修改因子标签
data$Months = factor(
data$Month,
levels = 5:9,
labels = c("May", "June", "July", "August", "September")
)
ggplot(data, aes(x=Wind, y=Temp.C, color=Months)) +
geom_point(show.legend=FALSE) +
geom_smooth(method="lm", show.legend=FALSE) +
facet_wrap(~ Months, nrow=1)`geom_smooth()` using formula = 'y ~ x'
ggplot(data, aes(x=Wind, y=Temp.C, color=Months)) +
geom_point(show.legend=FALSE) +
geom_smooth(method="lm", show.legend=FALSE) +
facet_wrap(~ Months, nrow=1, scales="free")`geom_smooth()` using formula = 'y ~ x'
facet_wrap():线性排列(卷饼🌯)
~ 分组变量facet_grid():两因素交叉组合排列(华夫饼🧇)
行分组变量 ~ 列分组变量## 两个分类变量的情况
## 数据变量计算复习(Chap 05 & 06)
data.bfi = as.data.table(psych::bfi)
data.bfi[, let(
Gender = factor(gender, levels=1:2, labels=c("Male", "Female")),
Age = as.numeric(age),
Edu = as.factor(education),
E = MEAN(data.bfi, "E", 1:5, rev=c(1,2), range=1:6),
A = MEAN(data.bfi, "A", 1:5, rev=1, range=1:6),
C = MEAN(data.bfi, "C", 1:5, rev=c(4,5), range=1:6),
N = MEAN(data.bfi, "N", 1:5, rev=NULL, range=1:6),
O = MEAN(data.bfi, "O", 1:5, rev=c(2,5), range=1:6)
)]
str(data.bfi)Classes 'data.table' and 'data.frame': 2800 obs. of 36 variables:
$ A1 : int 2 2 5 4 2 6 2 4 4 2 ...
$ A2 : int 4 4 4 4 3 6 5 3 3 5 ...
$ A3 : int 3 5 5 6 3 5 5 1 6 6 ...
$ A4 : int 4 2 4 5 4 6 3 5 3 6 ...
$ A5 : int 4 5 4 5 5 5 5 1 3 5 ...
$ C1 : int 2 5 4 4 4 6 5 3 6 6 ...
$ C2 : int 3 4 5 4 4 6 4 2 6 5 ...
$ C3 : int 3 4 4 3 5 6 4 4 3 6 ...
$ C4 : int 4 3 2 5 3 1 2 2 4 2 ...
$ C5 : int 4 4 5 5 2 3 3 4 5 1 ...
$ E1 : int 3 1 2 5 2 2 4 3 5 2 ...
$ E2 : int 3 1 4 3 2 1 3 6 3 2 ...
$ E3 : int 3 6 4 4 5 6 4 4 NA 4 ...
$ E4 : int 4 4 4 4 4 5 5 2 4 5 ...
$ E5 : int 4 3 5 4 5 6 5 1 3 5 ...
$ N1 : int 3 3 4 2 2 3 1 6 5 5 ...
$ N2 : int 4 3 5 5 3 5 2 3 5 5 ...
$ N3 : int 2 3 4 2 4 2 2 2 2 5 ...
$ N4 : int 2 5 2 4 4 2 1 6 3 2 ...
$ N5 : int 3 5 3 1 3 3 1 4 3 4 ...
$ O1 : int 3 4 4 3 3 4 5 3 6 5 ...
$ O2 : int 6 2 2 3 3 3 2 2 6 1 ...
$ O3 : int 3 4 5 4 4 5 5 4 6 5 ...
$ O4 : int 4 3 5 3 3 6 6 5 6 5 ...
$ O5 : int 3 3 2 5 3 1 1 3 1 2 ...
$ gender : int 1 2 2 2 1 2 1 1 1 2 ...
$ education: int NA NA NA NA NA 3 NA 2 1 NA ...
$ age : int 16 18 17 17 17 21 18 19 19 17 ...
$ Gender : Factor w/ 2 levels "Male","Female": 1 2 2 2 1 2 1 1 1 2 ...
$ Age : num 16 18 17 17 17 21 18 19 19 17 ...
$ Edu : Factor w/ 5 levels "1","2","3","4",..: NA NA NA NA NA 3 NA 2 1 NA ...
$ E : num 3.8 5 4.2 3.6 4.8 5.6 4.2 2.4 3.25 4.8 ...
$ A : num 4 4.2 3.8 4.6 4 4.6 4.6 2.6 3.6 5.4 ...
$ C : num 2.8 4 4 3 4.4 5.6 4.4 3.4 4 5.6 ...
$ N : num 2.8 3.8 3.6 2.8 3.2 3 1.4 4.2 3.6 4.2 ...
$ O : num 3 4 4.8 3.2 3.6 5 5.4 4.2 5 5.2 ...
- attr(*, ".internal.selfref")=<externalptr>
p = ggplot(data.bfi[Age>=18 & Age<60 & !is.na(Edu)],
aes(x=Age, y=E)) +
geom_point(alpha=0.1) +
geom_smooth(method="loess", color="firebrick", fill="orange") +
labs(y="Extraversion")
p`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
p1 = ggplot(data, aes(x=Temp.C)) +
geom_histogram(bins=10, color="black", fill="grey") +
labs(x="Temperature", y="Frequency")
p2 = ggplot(data, aes(x=Month, y=Temp.C)) +
geom_boxplot() +
labs(y="Temperature")
p12 = cowplot::plot_grid(p1, p2, nrow=1, labels="AUTO")
p12p3 = ggplot(data, aes(x=Wind, y=Temp.C)) +
geom_point() +
geom_smooth(method="lm") +
labs(y="Temperature")
p4 = ggplot(data, aes(x=Wind, y=Temp.C, color=Month)) +
geom_point(show.legend=FALSE) +
geom_smooth(method="lm", se=FALSE, show.legend=FALSE) +
facet_wrap(~ Month, nrow=1) +
labs(y="Temperature")
p1234 = cowplot::plot_grid(p1, p2, p3, p4, ncol=2, labels="AUTO")`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
作业要求:
cowplot::plot_grid()函数,把【作业9】完成的4张图拼合在一起,添加A~D标签,最后保存为合适尺寸的高清晰度图形文件(.png格式,清晰度dpi至少为300)平台提交: