如何用highchart R绘制多个y轴值?

tquggr8v  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(134)

我的数据集如下所示:

emissions<-structure(
  list(
    name = c(
      "China",
      "United States of America",
      "India",
      "Russia",
      "Japan",
      "China",
      "United States of America",
      "India",
      "Russia",
      "Japan",
      "China",
      "United States of America",
      "India",
      "Russia",
      "Japan"
    ),
    iso = c(
      "CHN",
      "USA",
      "IND",
      "RUS",
      "JPN",
      "CHN",
      "USA",
      "IND",
      "RUS",
      "JPN",
      "CHN",
      "USA",
      "IND",
      "RUS",
      "JPN"
    ),
    year = c(
      2019,
      2019,
      2019,
      2019,
      2019,
      2020,
      2020,
      2020,
      2020,
      2020,
      2021,
      2021,
      2021,
      2021,
      2021
    ),
    total = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    coal = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    oil = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    gas = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    cement = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    flaring = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    other = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10),
    per_capita = c(100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10, 100,
              75, 50, 25, 10)
  ),
  row.names = c(NA,-15L),
  class = c("data.table",
            "data.frame")
)

我提供的样本是一个例子,我有自1750年以来230多个国家的数据(排放量)。我试图用折线图描绘过去10年(2012年至2022年)每一类别(煤炭、石油、天然气、水泥、燃烧、其他)的排放量。
我不知道如何做到这一点。任何帮助将不胜感激。
谢谢你。

syqv5f0l

syqv5f0l1#

我想出了如何使用highchart创建这个:

past_10_years<-emissions %>% 
  select(name,year, coal, oil, gas,cement,flaring, other) %>% 
  filter(year>2011) %>% 
  filter(name=="Global")

view(past_10_years)


highchart() %>% 
  hc_xAxis(categories=past_10_years$year) %>% 
  hc_yAxis_multiples(
    list(lineWidth = 3),
    list(showLastLabel = FALSE, opposite = TRUE)
  ) %>% 
  hc_add_series(data = past_10_years$coal, name="Coal") %>% 
  hc_add_series(data = past_10_years$oil, name="Oil") %>% 
  hc_add_series(data = past_10_years$gas, name="Gas") %>% 
  hc_add_series(data = past_10_years$cement, name="Cement") %>%
  hc_add_series(data = past_10_years$flaring, name="Flaring") %>% 
  hc_add_series(data = past_10_years$others, name="Others",type = "spline", yAxis = 1) %>% 
  hc_title(text="Emissions in Past 10 Years",style = list(fontSize = "25px"))

注:全球排放量载于成为参照点的数据集中。

相关问题