You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Highcharts R绘制多Y轴排放类别折线图?

使用Highcharts R绘制多Y轴排放类别折线图解决方案

步骤1:安装并加载依赖包

# 首次运行时安装所需包
install.packages(c("highcharter", "dplyr", "tidyr"))

# 加载包
library(highcharter)
library(dplyr)
library(tidyr)

步骤2:数据预处理

筛选2012-2022年的数据,若需绘制全球趋势则按年份聚合各排放类别总和;若需绘制单个国家趋势,添加filter(name == "目标国家名称")即可。

# 筛选时间范围并聚合全球排放总量
filtered_data <- emissions %>%
  filter(year >= 2012 & year <= 2022) %>%
  group_by(year) %>%
  summarise(
    coal = sum(coal, na.rm = TRUE),
    oil = sum(oil, na.rm = TRUE),
    gas = sum(gas, na.rm = TRUE),
    cement = sum(cement, na.rm = TRUE),
    flaring = sum(flaring, na.rm = TRUE),
    other = sum(other, na.rm = TRUE)
  ) %>%
  ungroup()

步骤3:构建多Y轴折线图

通过配置多个Y轴对应不同排放类别,搭配折线系列实现可视化:

# 定义各排放类别配色(增强辨识度)
category_colors <- c(
  coal = "#000000",
  oil = "#8B4513",
  gas = "#4682B4",
  cement = "#A0522D",
  flaring = "#FF4500",
  other = "#32CD32"
)

# 创建图表
highchart() %>%
  hc_title(text = "2012-2022年全球各排放类别趋势") %>%
  hc_subtitle(text = "数据来源:自有排放数据集") %>%
  # 设置X轴为年份
  hc_xAxis(categories = filtered_data$year, title = list(text = "年份")) %>%
  # 配置6个独立Y轴,分别对应各排放类别
  hc_yAxis(
    # 煤炭排放轴(左侧)
    list(
      title = list(text = "煤炭排放", style = list(color = category_colors["coal"])),
      lineColor = category_colors["coal"],
      labels = list(style = list(color = category_colors["coal"]))
    ),
    # 石油排放轴(右侧)
    list(
      title = list(text = "石油排放", style = list(color = category_colors["oil"])),
      lineColor = category_colors["oil"],
      labels = list(style = list(color = category_colors["oil"])),
      opposite = TRUE
    ),
    # 天然气排放轴(左侧)
    list(
      title = list(text = "天然气排放", style = list(color = category_colors["gas"])),
      lineColor = category_colors["gas"],
      labels = list(style = list(color = category_colors["gas"]))
    ),
    # 水泥排放轴(右侧)
    list(
      title = list(text = "水泥排放", style = list(color = category_colors["cement"])),
      lineColor = category_colors["cement"],
      labels = list(style = list(color = category_colors["cement"])),
      opposite = TRUE
    ),
    # 燃除排放轴(左侧)
    list(
      title = list(text = "燃除排放", style = list(color = category_colors["flaring"])),
      lineColor = category_colors["flaring"],
      labels = list(style = list(color = category_colors["flaring"]))
    ),
    # 其他排放轴(右侧)
    list(
      title = list(text = "其他排放", style = list(color = category_colors["other"])),
      lineColor = category_colors["other"],
      labels = list(style = list(color = category_colors["other"])),
      opposite = TRUE
    )
  ) %>%
  # 添加各排放类别的折线系列,绑定对应Y轴
  hc_add_series(data = filtered_data$coal, name = "煤炭", type = "line", color = category_colors["coal"], yAxis = 0) %>%
  hc_add_series(data = filtered_data$oil, name = "石油", type = "line", color = category_colors["oil"], yAxis = 1) %>%
  hc_add_series(data = filtered_data$gas, name = "天然气", type = "line", color = category_colors["gas"], yAxis = 2) %>%
  hc_add_series(data = filtered_data$cement, name = "水泥", type = "line", color = category_colors["cement"], yAxis = 3) %>%
  hc_add_series(data = filtered_data$flaring, name = "燃除", type = "line", color = category_colors["flaring"], yAxis = 4) %>%
  hc_add_series(data = filtered_data$other, name = "其他", type = "line", color = category_colors["other"], yAxis = 5) %>%
  # 配置交互功能
  hc_legend(enabled = TRUE) %>%
  hc_tooltip(shared = TRUE, crosshairs = TRUE) %>%
  # 使用简洁主题
  hc_theme(hc_theme_smpl())

关键注意事项

  • 单个国家趋势:在filtered_data的筛选步骤中添加filter(name == "China")(替换为目标国家),无需聚合。
  • 轴位置调整:通过opposite参数设置Y轴在左侧/右侧,避免标签重叠。
  • 缺失值处理:sum(..., na.rm = TRUE)会自动忽略缺失值,保证计算准确性。

内容的提问来源于stack exchange,提问作者Shashivydyula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 19:50:17