如何在R中绘制10年数据的GDP、通胀率、失业率多变量趋势图
实现多变量趋势同图的解决方案
嘿,我来帮你搞定把GDP、Inflation_Rate、Unemployment_Rate三个变量放在同一图里的需求!首先先把你的数据集整理成清晰的表格方便查看:
| 序号 | Country | Year | FY_sales | Truck_type | GDP | Inflation_Rate | Unemployment_Rate |
|---|---|---|---|---|---|---|---|
| 1 | France | 2007-05-25 | 2064543 | LCV | 2663112510266 | 1.488073528 | 7.659999847 |
| 2 | France | 2007-05-25 | 460552 | MCV/CV | 2663112510266 | 1.488073528 | 7.659999847 |
| 3 | France | 2007-05-25 | 58940 | HCV | 2663112510266 | 1.488073528 | 7.659999847 |
能看到这三个指标的数值范围差异极大,直接用同一Y轴的话,通胀率和失业率的趋势会被GDP完全掩盖。参考你想要的示例图样式,我们可以用双Y轴的方式来解决,或者把数据标准化后用同一轴展示相对趋势,下面给你两种具体实现方案:
方案1:双Y轴展示(保留原数值)
这个方案和你参考的图样式一致,左轴显示通胀率和失业率,右轴显示GDP,通过缩放因子把GDP的数值匹配到左轴的量级,再在次轴还原回原数值:
# 加载必要的包 library(ggplot2) library(dplyr) # 计算缩放因子:把GDP的最大值缩到和另外两个指标最大值相近的范围 scale_factor <- max(data$GDP) / max(c(data$Inflation_Rate, data$Unemployment_Rate)) # 绘制双轴趋势图 ggplot(data, aes(x = Year)) + # 绘制GDP的折线和散点(已缩放,对应右轴) geom_line(aes(y = GDP / scale_factor, color = "GDP"), linewidth = 1) + geom_point(aes(y = GDP / scale_factor, color = "GDP"), size = 2) + # 绘制通胀率的折线和散点(对应左轴) geom_line(aes(y = Inflation_Rate, color = "Inflation Rate"), linewidth = 1) + geom_point(aes(y = Inflation_Rate, color = "Inflation Rate"), size = 2) + # 绘制失业率的折线和散点(对应左轴) geom_line(aes(y = Unemployment_Rate, color = "Unemployment Rate"), linewidth = 1) + geom_point(aes(y = Unemployment_Rate, color = "Unemployment Rate"), size = 2) + # 设置Y轴:左轴对应通胀/失业率,右轴还原GDP原数值 scale_y_continuous( name = "Inflation Rate / Unemployment Rate", sec.axis = sec_axis(~ . * scale_factor, name = "GDP") ) + # 设置颜色区分不同指标 scale_color_manual(values = c("GDP" = "#1f77b4", "Inflation Rate" = "#ff7f0e", "Unemployment Rate" = "#2ca02c")) + # 添加标题和标签 labs( title = "Trend of GDP, Inflation Rate and Unemployment Rate", x = "Year", color = "Indicator" ) + # 优化主题样式 theme_minimal() + theme( plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), axis.title = element_text(size = 12), legend.position = "top" )
代码说明:
- 缩放因子的作用是让GDP的趋势在左轴上能和另外两个指标的趋势对齐,次轴再把缩放后的数值还原成GDP的真实量级
- 每个指标用不同颜色区分,方便快速识别
- 当你的数据有多个年份时,就能呈现出像参考图那样的趋势变化啦
方案2:标准化后展示相对趋势
如果你不需要保留原数值的直观性,只想看三个指标的相对变化趋势,可以把数据标准化(比如Z-score标准化),这样所有指标都在同一个尺度上:
# 加载必要的包 library(ggplot2) library(dplyr) library(tidyr) # 标准化数据并转为长格式 normalized_data <- data %>% select(Year, GDP, Inflation_Rate, Unemployment_Rate) %>% mutate( # 对每个指标做Z-score标准化 GDP_norm = scale(GDP)[,1], Inflation_norm = scale(Inflation_Rate)[,1], Unemployment_norm = scale(Unemployment_Rate)[,1] ) %>% # 转为长格式,方便ggplot绘图 pivot_longer( cols = ends_with("_norm"), names_to = "Indicator", values_to = "Normalized_Value" ) %>% # 清理指标名称 mutate(Indicator = gsub("_norm", "", Indicator)) # 绘制标准化趋势图 ggplot(normalized_data, aes(x = Year, y = Normalized_Value, color = Indicator)) + geom_line(linewidth = 1) + geom_point(size = 2) + labs( title = "Normalized Trend of GDP, Inflation Rate and Unemployment Rate", x = "Year", y = "Normalized Value (Z-score)", color = "Indicator" ) + scale_color_manual(values = c("GDP" = "#1f77b4", "Inflation Rate" = "#ff7f0e", "Unemployment Rate" = "#2ca02c")) + theme_minimal() + theme( plot.title = element_text(hjust = 0.5, size = 14, face = "bold"), axis.title = element_text(size = 12), legend.position = "top" )
这个方案的好处是不用处理双轴的缩放逻辑,缺点是无法直接看到指标的真实数值,只能对比相对变化。
内容的提问来源于stack exchange,提问作者prasanth
相关产品推荐
相关产品推荐

