用R语言绘制泰坦尼克号乘客的生存与性别的频数表。 1.以生存为分组的分组图 2.以性别为分组的分组图 3.以性别为分组的堆积图 4.用分面函数绘制不同船舱等级的生存与性别的频数表 5.绘制包含误差线的直方图,比较三等舱女性和一等舱男性的存活率否存在差异
时间: 2025-01-04 12:40:32 浏览: 6
在R语言中,我们可以使用ggplot2包来绘制泰坦尼克号乘客的生存与性别的频数表。以下是各个图表的制作步骤:
1. **以生存为分组的分组图**:
```r
library(ggplot2)
data <- read.csv("titanic_data.csv") # 假设数据来自CSV文件
gender_survival <- ggplot(data, aes(x = factor(Survived), fill = factor(Gender))) +
geom_bar(position = "dodge") +
labs(title = "泰坦尼克幸存者按性别分布", x = "生存状态", y = "人数")
gender_survival
```
2. **以性别为分组的分组图**:
```r
gender_count <- ggplot(data, aes(x = Gender, fill = factor(Survived))) +
geom_bar() +
labs(title = "泰坦尼克乘客按性别及生存状态", x = "性别", y = "人数")
gender_count
```
3. **以性别为分组的堆积图**:
```r
gender_stacked <- ggplot(data, aes(x = Gender, fill = factor(Survived))) +
geom_bar(position = "fill") +
labs(title = "泰坦尼克乘客性别堆叠的生存情况", x = "性别", y = "人数")
gender_stacked
```
4. **用分面函数绘制不同船舱等级的生存与性别的频数表**:
```r
cabin_gender <- ggplot(data, aes(x = factor(Cabin), fill = factor(Survived), color = factor(Gender))) +
geom_tile() +
facet_wrap(~ Gender) +
labs(title = "不同船舱等级下乘客的生存状况", x = "船舱等级", y = "")
cabin_gender
```
5. **绘制包含误差线的直方图,比较三等舱女性和一等舱男性的存活率**:
```r
female_male_diff <- ggplot(data, aes(x = factor(Pclass), y = (n female)/(n total), group = Gender)) +
geom_errorbar(aes(ymin = (n female - 0.5 * sd female) / (n total), ymax = (n female + 0.5 * sd female) / (n total)), width = 0.2) +
geom_point(color = c("red" for male, "blue" for female)) +
labs(title = "三等舱与一等舱女性和男性存活率对比", x = "船舱等级", y = "%存活")
female_male_diff
```
记得替换`titanic_data.csv`为实际数据文件名,并根据需要调整数据加载、变量选择和图形细节。以上代码示例假设数据集中有'Survived', 'Gender', 'Cabin', 和 'Pclass' 等列。
阅读全文