##问题1
# 加载所需包
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.5.2
library(plotly)
## Warning: package 'plotly' was built under R version 4.5.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.5.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# 1. 查看gapminder数据结构与内容
data(gapminder) # 加载数据
str(gapminder) # 查看数据结构
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
head(gapminder) # 查看前6行数据
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
summary(gapminder) # 数据基本统计描述
## country continent year lifeExp
## Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60
## Albania : 12 Americas:300 1st Qu.:1966 1st Qu.:48.20
## Algeria : 12 Asia :396 Median :1980 Median :60.71
## Angola : 12 Europe :360 Mean :1980 Mean :59.47
## Argentina : 12 Oceania : 24 3rd Qu.:1993 3rd Qu.:70.85
## Australia : 12 Max. :2007 Max. :82.60
## (Other) :1632
## pop gdpPercap
## Min. :6.001e+04 Min. : 241.2
## 1st Qu.:2.794e+06 1st Qu.: 1202.1
## Median :7.024e+06 Median : 3531.8
## Mean :2.960e+07 Mean : 7215.3
## 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
## Max. :1.319e+09 Max. :113523.1
##
# 2. plotly绘制交互式趋势图:中美印人均GDP随年份变化
p1 <- gapminder %>%
filter(country %in% c("China", "United States", "India")) %>%
plot_ly(x = ~year, y = ~gdpPercap, color = ~country,
type = "scatter", mode = "lines+markers",
line = list(width = 2), marker = list(size = 8)) %>%
layout(title = list(text = "中美印人均GDP趋势对比", font = list(size = 16)),
xaxis = list(title = "年份", titlefont = list(size = 14)),
yaxis = list(title = "人均GDP(美元)", titlefont = list(size = 14)),
legend = list(title = list(text = "国家"), font = list(size = 12)))
# 3. plotly绘制交互式箱线图:2007年各洲预期寿命分布
p2 <- gapminder %>%
filter(year == 2007) %>%
plot_ly(x = ~continent, y = ~lifeExp, color = ~continent,
type = "box", boxpoints = "all", jitter = 0.3,
pointpos = -1.8, alpha = 0.7) %>%
layout(title = list(text = "2007年各洲预期寿命分布", font = list(size = 16)),
xaxis = list(title = "大洲", titlefont = list(size = 14)),
yaxis = list(title = "预期寿命(岁)", titlefont = list(size = 14)),
showlegend = FALSE)
# 4. plotly绘制交互式堆叠面积图:各洲年度GDP总和变化
gdp_data <- gapminder %>%
mutate(gdp_total = gdpPercap * pop) %>%
group_by(year, continent) %>%
summarise(total_gdp = sum(gdp_total)/1e12, .groups = "drop")
p3 <- plot_ly(gdp_data, x = ~year, y = ~total_gdp, color = ~continent,
type = "scatter", mode = "none", stackgroup = "one",
fillcolor = "rgba(255,255,255,0.5)") %>%
layout(title = list(text = "全球各洲年度GDP总和变化(万亿美元)", font = list(size = 16)),
xaxis = list(title = "年份", titlefont = list(size = 14)),
yaxis = list(title = "GDP总和(万亿美元)", titlefont = list(size = 14)),
legend = list(title = list(text = "大洲"), font = list(size = 12)))
# 5. plotly绘制交互式散点图:2007年人均GDP与预期寿命关系(按人口缩放)
p4 <- gapminder %>%
filter(year == 2007) %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp, color = ~continent,
size = ~pop, sizes = c(10, 500), # 人口规模对应点大小范围
type = "scatter", mode = "markers", alpha = 0.7,
hoverinfo = "text",
text = ~paste("国家:", country, "<br>人均GDP:", round(gdpPercap, 2),
"<br>预期寿命:", round(lifeExp, 1), "<br>人口:", format(pop, big.mark = ","))) %>%
layout(title = list(text = "2007年人均GDP与预期寿命关系", font = list(size = 16)),
xaxis = list(title = "人均GDP(美元)", type = "log", titlefont = list(size = 14)),
yaxis = list(title = "预期寿命(岁)", titlefont = list(size = 14)),
legend = list(title = list(text = "大洲"), font = list(size = 12)))
# 显示图形(可逐个运行p1/p2/p3/p4查看)
p1
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
p2
p3
p4
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.