# 使用USArrests数据集
data(USArrests)
USArrests_scaled <- as.data.frame(scale(USArrests))
USArrests_scaled$State <- rownames(USArrests)
# 准备平行坐标图数据
parallel_data <- USArrests_scaled %>%
gather(key = "Variable", value = "Value", -State) %>%
left_join(USArrests_scaled %>% select(State, Murder), by = "State")
# 图形4 - 修正后的平行坐标图
ggplot(parallel_data, aes(x = Variable, y = Value, group = State, color = Murder)) +
geom_line(alpha = 0.6) +
scale_color_viridis(option = "magma") +
labs(title = "图形4: 美国各州逮捕率平行坐标图",
x = "逮捕率类型",
y = "标准化值",
color = "谋杀率") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))