library(tidyverse)
library(gapminder)
library(palmerpenguins)
library(DT)
library(plotly)
library(ggridges)
library(ggcorrplot)
library(patchwork)
library(viridis)
library(ggrepel)
library(lubridate)
library(heatmaply)
data(gapminder)
data(penguins)
<- gapminder %>%
combined_data filter(year == 2007) %>%
left_join(
%>%
penguins group_by(species) %>%
summarise(
avg_bill_length = mean(bill_length_mm, na.rm = TRUE),
avg_bill_depth = mean(bill_depth_mm, na.rm = TRUE),
avg_flipper_length = mean(flipper_length_mm, na.rm = TRUE),
avg_body_mass = mean(body_mass_g, na.rm = TRUE)
%>%
) mutate(continent = case_when(
== "Adelie" ~ "Oceania",
species == "Chinstrap" ~ "Antarctica",
species == "Gentoo" ~ "Antarctica"
species
)),by = "continent"
)
<- gapminder %>%
time_series_data select(country, continent, year, lifeExp, gdpPercap) %>%
complete(country, year = 1952:2007) %>%
group_by(country) %>%
mutate(
continent = first(na.omit(continent)),
lifeExp = approx(year, lifeExp, year, method = "linear")$y,
gdpPercap = approx(year, gdpPercap, year, method = "linear")$y
%>%
) ungroup()
数据可视化期末报告
1 报告要求
期末实验报告由5章节5个图形组成,每个章节需要作一个图形。
每个章节选择作什么图自主选择,作图前补充完整图形标题名称,例如:图形1——多变量条形图。
案例数据自主收集,不同章节可以公用一个数据集。但同学间不允许使用相同数据集。
每个章节的数据集合需要通过
datatable
函数展示,并简要解释数据来源和变量意义。每个输出图形后需要对图形作简要解读,最少需针对图形提出一个观点。
渲染html文件保留代码展示,6月22日前将发布网址提交至共享文档
“8、期末报告”
列中。评分标准:
每章节图形各20分
能有效输出图形和合理解释75%
数据独特性强10%
图形个性化强15%
2 类别数据可视化
2.1 案例数据解释与展示
gapminder
数据集,包含各大洲国家的基本发展指标。continent: 大洲名称
avg_lifeExp: 平均预期寿命
avg_gdpPercap: 人均GDP平均值
count: 国家数量
<- combined_data %>%
category_data group_by(continent) %>%
summarise(
avg_lifeExp = mean(lifeExp),
avg_gdpPercap = mean(gdpPercap),
count = n()
)
datatable(category_data,
caption = "表1:各大洲平均寿命与GDP数据",
options = list(pageLength = 5))
2.2 图形1——南丁格尔玫瑰图
<- ggplot(category_data, aes(x = continent, y = avg_gdpPercap, fill = avg_lifeExp)) +
rose_plot geom_bar(width = 1, stat = "identity", color = "white") +
coord_polar(theta = "x", start = 0) +
scale_fill_viridis(name = "平均寿命", option = "magma") +
labs(title = "图形1:各大洲人均GDP与平均寿命南丁格尔玫瑰图",
subtitle = "面积表示人均GDP,颜色表示平均寿命",
x = NULL, y = NULL) +
theme_minimal() +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(size = 14, face = "bold"),
legend.position = "right")
rose_plot
图形解读:南丁格尔玫瑰图直观展示了大洲间的发展差异。从图中可以看出:
欧洲国家的人均GDP最高,同时平均寿命也最长
非洲国家在人均GDP和平均寿命两方面都显著落后
亚洲国家虽然数量众多,但人均GDP和平均寿命表现中等
经济发展水平(人均GDP)与健康水平(平均寿命)呈现明显的正相关关系。
3 数据分布可视化
3.1 案例数据解释与展示
gapminder
和palmerpenguins
组合数据。continent: 大洲名称
variable: 变量名称(寿命、GDP、企鹅平均体重)
value: 变量值
<- combined_data %>%
distribution_data select(continent, lifeExp, gdpPercap, avg_body_mass) %>%
pivot_longer(cols = -continent, names_to = "variable", values_to = "value")
datatable(distribution_data %>% sample_n(10),
caption = "表2:数据分布分析样本数据",
options = list(pageLength = 5))
3.2 图形2——云雨图
<- ggplot(distribution_data, aes(x = continent, y = value, fill = continent)) +
raincloud_plot ::stat_halfeye(adjust = 0.5, width = 0.6, .width = 0, justification = -0.3, point_colour = NA) +
ggdistgeom_boxplot(width = 0.15, outlier.shape = NA, alpha = 0.5) +
::geom_half_point(side = "l", range_scale = 0.3, alpha = 0.5, size = 1.5) +
gghalvesfacet_wrap(~variable, scales = "free_y", ncol = 1) +
scale_fill_brewer(palette = "Set2") +
labs(title = "图形2:各变量在大洲间的分布云雨图",
subtitle = "结合了密度曲线、箱线图和原始数据点",
x = "大洲", y = "值") +
theme_minimal() +
theme(legend.position = "none",
plot.title = element_text(size = 14, face = "bold"),
strip.text = element_text(face = "bold"))
raincloud_plot
图形解读:云雨图综合展示了三种可视化元素,全面呈现数据分布特征:
底部点图显示原始数据分布
中间箱线图展示四分位数和异常值
顶部密度曲线展示数据整体分布形状
非洲国家的寿命分布呈现明显的右偏态,而欧洲国家则更为对称,表明非洲国家间寿命差异较大。
4 变量关系可视化
4.1 案例数据解释与展示
组合数据集。
country: 国家名称
continent: 大洲名称
lifeExp: 预期寿命
gdpPercap: 人均GDP
pop: 人口数量
avg_flipper_length: 企鹅平均鳍状肢长度
pop_size: 人口规模分组
<- combined_data %>%
relation_data select(country, continent, lifeExp, gdpPercap, pop, avg_flipper_length) %>%
mutate(pop_size = cut_number(pop, 3))
datatable(relation_data %>% sample_n(10),
caption = "表3:变量关系分析样本数据",
options = list(pageLength = 5))
4.2 图形3——气泡图
<- ggplot(relation_data,
bubble_plot aes(x = gdpPercap, y = lifeExp,
size = pop, color = continent,
text = paste("国家:", country))) +
geom_point(alpha = 0.7) +
scale_size(range = c(2, 15), name = "人口数量") +
scale_color_brewer(palette = "Set1") +
scale_x_log10(labels = scales::dollar) +
labs(title = "图形3:人均GDP与预期寿命关系气泡图",
subtitle = "气泡大小表示人口数量,颜色表示大洲",
x = "人均GDP(对数尺度)", y = "预期寿命") +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold"),
legend.position = "bottom")
# 交互式气泡图
ggplotly(bubble_plot, tooltip = "text") %>%
layout(hoverlabel = list(bgcolor = "white"))
图形解读:气泡图展示了四个变量的关系:
X轴:人均GDP(对数尺度)
Y轴:预期寿命
气泡大小:人口数量
颜色:所属大洲
人均GDP与预期寿命存在明显的正相关关系,但当人均GDP超过约10,000美元后,预期寿命的增长趋于平缓,表明经济发展对健康的影响存在边际递减效应。
5 样本相似性可视化
5.1 案例数据解释与展示
continent: 大洲名称
lifeExp: 标准化后的平均预期寿命
gdpPercap: 标准化后的人均GDP
pop: 标准化后的中位数人口
country_count: 标准化后的国家数量
<- combined_data %>%
similarity_data group_by(continent) %>%
summarise(
lifeExp = mean(lifeExp, na.rm = TRUE),
gdpPercap = mean(gdpPercap, na.rm = TRUE),
pop = median(pop, na.rm = TRUE),
country_count = n_distinct(country, na.rm = TRUE)
%>%
) ungroup() %>%
mutate(across(-continent, scales::rescale))
datatable(similarity_data,
caption = "表4:大洲特征标准化数据(修正版)",
options = list(pageLength = 5))
5.2 图形4——平行坐标图
<- similarity_data %>%
parallel_plot pivot_longer(cols = -continent, names_to = "variable", values_to = "value") %>%
ggplot(aes(x = variable, y = value, group = continent, color = continent)) +
geom_line(size = 1.5, alpha = 0.8) +
geom_point(size = 3) +
scale_color_brewer(palette = "Set1") +
labs(title = "图形4:各大洲多特征平行坐标图",
subtitle = "标准化后的各项指标对比",
x = "变量", y = "标准化值",
color = "大洲") +
theme_minimal() +
theme(plot.title = element_text(size = 14, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.position = "bottom") +
scale_x_discrete(labels = c("country_count" = "国家数量",
"gdpPercap" = "人均GDP",
"lifeExp" = "预期寿命",
"pop" = "人口"))
parallel_plot
图形解读:平行坐标图清晰地展示了大洲间在不同指标上的相对表现:
欧洲在人均GDP和预期寿命上表现突出
非洲在人口和国家数量上相对较高,但在经济和生活质量指标上落后
美洲各项指标相对均衡
平行坐标图揭示了发展水平与人口规模之间的反向关系,人口较多的地区往往发展指标较低,这可能是资源分配问题的可视化体现。
6 时间序列可视化
6.1 案例数据解释与展示
gapminder
数据集扩展的时间序列。country: 国家名称
continent: 大洲名称
year: 年份
lifeExp: 预期寿命
gdpPercap: 人均GDP
datatable(time_series_data %>% sample_n(10),
caption = "表5:时间序列数据样本",
options = list(pageLength = 5))
6.2 图形5——流线图
<- c("China", "United States", "India", "Brazil", "South Africa", "Germany")
selected_countries
<- time_series_data %>%
stream_plot filter(country %in% selected_countries) %>%
ggplot(aes(x = year, y = lifeExp, color = country)) +
geom_line(size = 1.5, alpha = 0.8) +
geom_point(size = 2) +
geom_text_repel(
data = . %>% filter(year == max(year)),
aes(label = country),
hjust = 0, direction = "y", nudge_x = 1
+
) scale_color_brewer(palette = "Set1") +
scale_x_continuous(limits = c(1952, 2015)) +
labs(title = "图形5:代表性国家预期寿命变化流线图",
subtitle = "1952-2007年期间变化趋势",
x = "年份", y = "预期寿命") +
theme_minimal() +
theme(legend.position = "none",
plot.title = element_text(size = 14, face = "bold"),
panel.grid.minor = element_blank())
stream_plot
图形解读:流线图展示了六个代表性国家半个多世纪的寿命变化:
所有国家都呈现寿命增长趋势
中国和印度等发展中国家增长幅度更大
发达国家如美国、德国起点高但增长相对平缓
发展中国家在20世纪后期的寿命提升速度明显快于发达国家,反映了全球健康不平等的缩小趋势。