r语言绘制基于Class和Survived 的矩形树状图代码
时间: 2024-05-09 12:17:29 浏览: 12
以下是基于Class和Survived的矩形树状图(rectangular tree)的r语言代码:
```r
# 读取数据
data(titanic)
# 安装和载入需要的 packages
install.packages("party")
library(party)
# 构建决策树
tree <- ctree(Survived ~ Class, data = titanic)
# 绘制矩形树状图
plot(tree, type = "simple")
```
希望这段代码对您有帮助。如果您有任何其他问题或疑问,欢迎随时向我提出。
相关问题
使用R自带数据集Titanic,绘制一下图形:(1)(4)绘制Class、Sex、Age和Survived4个变量的条形树状图和矩形树状图;(2)绘制Class和Survived的气球图、热图和南丁格尔玫瑰图
好的,我会尽力回答你的问题。首先,我需要确认一下您的问题,您是想让我使用R自带数据集Titanic,绘制一下图形,包括:
1. 绘制Class、Sex、Age和Survived4个变量的条形树状图和矩形树状图;
2. 绘制Class和Survived的气球图、热图和南丁格尔玫瑰图。
对吗?如果是这样的话,那么我可以给您提供一些代码和图形,帮助您更好地理解和实现这些图形。
首先,我们需要安装一些库,包括ggplot2、ggpubr、dplyr、reshape2和ggalluvial。您可以使用以下代码安装它们:
```
install.packages("ggplot2")
install.packages("ggpubr")
install.packages("dplyr")
install.packages("reshape2")
install.packages("ggalluvial")
```
安装完成后,我们可以加载这些库:
```
library(ggplot2)
library(ggpubr)
library(dplyr)
library(reshape2)
library(ggalluvial)
```
接下来,我们需要加载Titanic数据集:
```
data(Titanic)
```
现在,我们可以开始绘制图形了。首先,让我们来绘制条形树状图和矩形树状图。代码如下:
```R
# 计算每个组合的总数
titanic_count <- as.data.frame(Titanic) %>%
mutate(Count = Freq) %>%
select(-Freq) %>%
group_by(Class, Sex, Age, Survived) %>%
summarise(Count = sum(Count))
# 绘制条形树状图
ggplot(titanic_count, aes(x = Class, y = Count, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(~ Age + Survived) +
ggtitle("Titanic Passenger Count by Class, Sex, Age and Survived")
# 绘制矩形树状图
ggplot(titanic_count, aes(x = Sex, y = Count, fill = Class)) +
geom_bar(stat = "identity", position = "fill") +
facet_grid(~ Age + Survived) +
ggtitle("Titanic Passenger Count by Sex, Class, Age and Survived")
```
这里我们使用了ggplot2库来绘制图形。我们首先计算了每个组合的总数,并将结果存储在titanic_count数据框中。然后,我们使用ggplot函数来绘制条形树状图和矩形树状图。对于条形树状图,我们将Class作为x轴,Count作为y轴,Sex作为填充,并使用facet_grid函数按Age和Survived进行分面。对于矩形树状图,我们将Sex作为x轴,Count作为y轴,Class作为填充,并使用facet_grid函数按Age和Survived进行分面。
接下来,让我们来绘制气球图、热图和南丁格尔玫瑰图。代码如下:
```R
# 计算每个组合的总数
titanic_count <- as.data.frame(Titanic) %>%
mutate(Count = Freq) %>%
select(-Freq) %>%
group_by(Class, Survived) %>%
summarise(Count = sum(Count))
# 绘制气球图
ggplot(titanic_count, aes(x = Class, y = Count, fill = Survived)) +
geom_point(stat = "identity", size = 10, alpha = 0.7) +
labs(title = "Titanic Passenger Count by Class and Survived",
subtitle = "Bubble Chart",
x = "Class",
y = "Count",
fill = "Survived") +
theme_minimal()
# 绘制热图
ggplot(titanic_count, aes(x = Class, y = Survived, fill = Count)) +
geom_tile() +
labs(title = "Titanic Passenger Count by Class and Survived",
subtitle = "Heatmap",
x = "Class",
y = "Survived",
fill = "Count") +
theme_minimal()
# 绘制南丁格尔玫瑰图
titanic_count$Class <- reorder(titanic_count$Class, titanic_count$Count)
ggplot(titanic_count, aes(x = "", y = Count, fill = Survived)) +
geom_bar(width = 1, stat = "identity") +
coord_polar(theta = "y") +
labs(title = "Titanic Passenger Count by Class and Survived",
subtitle = "Rose Chart",
x = "",
y = "Count",
fill = "Survived") +
theme_minimal() +
facet_wrap(~ Class, ncol = 3)
```
这里我们先计算了每个组合的总数,并将结果存储在titanic_count数据框中。然后,我们使用ggplot函数来绘制气球图、热图和南丁格尔玫瑰图。对于气球图,我们将Class作为x轴,Count作为y轴,Survived作为填充,并使用geom_point函数绘制点。对于热图,我们将Class作为x轴,Survived作为y轴,Count作为填充,并使用geom_tile函数绘制矩形。对于南丁格尔玫瑰图,我们首先使用reorder函数将Class按Count排序,然后将Class作为facet_wrap函数的参数,以便为每个Class绘制一个子图。我们使用geom_bar函数绘制条形,并使用coord_polar函数将图形转换为南丁格尔玫瑰图。
希望这些代码和图形能够帮助您更好地理解和实现您的问题。如果您有任何疑问,请随时问我。
使用R自带的数据集Titanic绘制绘制Class和Survived的气球图、热图和南丁格尔玫瑰图
好的,我可以为您提供代码来绘制Titanic数据集的Class和Survived的气球图、热图和南丁格尔玫瑰图。请注意,需要安装`ggplot2`和`ggforce`包来绘制气球图和南丁格尔玫瑰图。
首先,加载数据集并准备数据:
```R
library(ggplot2)
library(ggforce)
# 加载数据集
data(Titanic)
# 将数据转换为数据框
df <- as.data.frame(Titanic)
# 将数据格式转换为适合绘制气球图和南丁格尔玫瑰图的格式
df <- df %>%
mutate(Survived = factor(Survived),
Class = factor(Class),
n = as.numeric(Freq))
```
接下来,绘制气球图:
```R
# 绘制气球图
ggplot(df, aes(x = Class, y = Survived, size = n, fill = Survived)) +
geom_circle(aes(r = n/2), alpha = 0.8, color = "white", show.legend = FALSE) +
scale_size(range = c(0, 1), guide = FALSE) +
labs(title = "Titanic Survival by Class", x = "Class", y = "Survived") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5))
```
绘制热图:
```R
# 绘制热图
ggplot(df, aes(x = Class, y = Survived, fill = n)) +
geom_tile(color = "white") +
scale_fill_gradient(low = "white", high = "steelblue") +
labs(title = "Titanic Survival by Class", x = "Class", y = "Survived") +
theme(plot.title = element_text(hjust = 0.5))
```
绘制南丁格尔玫瑰图:
```R
# 绘制南丁格尔玫瑰图
ggplot(df, aes(x = Class, y = n, fill = Survived)) +
geom_bar_polar(aes(y = n/4, theta = -2*pi*(Class-1)/4 + pi/2), stat = "identity") +
scale_fill_manual(values = c("#E7B800", "#FC4E07")) +
labs(title = "Titanic Survival by Class", x = "Class", y = "Count", fill = "Survived") +
theme(plot.title = element_text(hjust = 0.5))
```
希望这些代码可以帮助您绘制出Titanic数据集的Class和Survived的气球图、热图和南丁格尔玫瑰图。