一 、(20 分,每小题 5 分) 给定数据框 df <- data.frame( x = rnorm(120, c(0, 2, 4)), y = rnorm(120, c(1, 2, 1)), z = letters[1:3] ) 其中x 表示数据的横坐标,y 表示数据纵坐标,z 表示类别(a,b,c 三类)。 (1) 将 a,b,c 三类,画在一张图上,并用颜色区分类别。 (2) 将 a,b,c 三类,分别画在并列排放的三张图上(点用黑色)。 (3) 在(2)的基础上,每张子图上再加上每类点的质心(质心用颜色区分,size=4)。 (4) 将 a,b,c 三类,分别画在并列排放的三张图上(点用彩色)。每张子图上,其他 数据用灰色(”grey70”)显示。(例如:第一张子图 a 类数据是彩色,则 b 和 c 类 数据是灰色。) 二 、(20 分,每小题 5 分) 学校想了解上习题课对学生考试成绩(scores)的影响。有 A,B,C 三位学生,一
时间: 2024-03-16 22:45:58 浏览: 28
1.
```R
# 1.1
library(ggplot2)
ggplot(df, aes(x = x, y = y, color = z)) + geom_point()
# 1.2
ggplot(df, aes(x = x, y = y)) + geom_point(data = subset(df, z == "a"), color = "black") +
ggtitle("A") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "b"), color = "black") +
ggtitle("B") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "c"), color = "black") +
ggtitle("C") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y")
# 1.3
ggplot(df, aes(x = x, y = y)) + geom_point(data = subset(df, z == "a"), color = "black") +
ggtitle("A") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "b"), color = "black") +
ggtitle("B") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "c"), color = "black") +
ggtitle("C") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = aggregate(df[, 1:2], list(df$z), mean), aes(x = x, y = y, color = Group.1), size = 4)
# 1.4
ggplot(df, aes(x = x, y = y)) + geom_point(data = subset(df, z == "a"), color = "red") +
ggtitle("A") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "b"), color = "blue") +
ggtitle("B") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "c"), color = "green") +
ggtitle("C") + theme(plot.title = element_text(hjust = 0.5, size = 20)) +
coord_cartesian(xlim = c(-3, 5), ylim = c(-3, 6)) +
labs(x = "X", y = "Y") +
geom_point(data = subset(df, z == "a"), color = "black") +
geom_point(data = subset(df, z == "b"), color = "black") +
geom_point(data = subset(df, z == "c"), color = "black") +
geom_point(data = aggregate(df[, 1:2], list(df$z), mean), aes(x = x, y = y, color = Group.1), size = 4) +
scale_color_manual(values = c("red", "blue", "green"))
```
2.
```R
# 2.1
df2 <- data.frame(scores = c(70, 75, 80, 85, 90, 95, 100),
A = c(110, 120, 130, 140, 150, 160, 170),
B = c(90, 100, 110, 120, 130, 140, 150),
C = c(70, 80, 90, 100, 110, 120, 130))
# 2.2
library(tidyr)
library(ggplot2)
df2_long <- gather(df2, key = "Student", value = "Scores", -scores)
ggplot(df2_long, aes(x = scores, y = Scores, fill = Student)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("red", "blue", "green")) +
labs(x = "Scores", y = "Exam Performance") +
theme(legend.title = element_blank())
# 2.3
df2_long <- df2_long %>%
group_by(Student) %>%
summarize(mean_score = mean(Scores))
ggplot(df2_long, aes(x = Student, y = mean_score, fill = Student)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("red", "blue", "green")) +
labs(x = "Student", y = "Mean Exam Performance") +
theme(legend.position = "none")
# 2.4
ggplot(df2_long, aes(x = Student, y = mean_score, fill = Student)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("red", "blue", "green")) +
labs(x = "Student", y = "Mean Exam Performance") +
theme(legend.position = "none") +
geom_hline(yintercept = mean(df2$Scores), linetype = "dashed", color = "black") +
annotate("text", x = 2, y = mean(df2$Scores) + 5, label = "Overall Mean", size = 5, fontface = "bold")
```