在R/Highcharts中是否要在柱形图中显示此输出?

pxq42qpu  于 2022-12-13  发布在  Highcharts
关注(0)|答案(1)|浏览(177)

我想用highchart创建此图表。

这个想法是,每个国家的较小的列在较大的列之内。
我如何才能做到这一点?
这是我的代码:

library(tidyverse)
library(gapminder)

gapminder::gapminder %>%
  
  filter(year >= 1990,country %in% c('Chile','Argentina'))%>%     
  
  hchart(
    type = 'column',
    
    hcaes(x = year, y = gdpPercap, group = country),
    visible = TRUE,
    showInLegend = TRUE,
    name  = "",
    zIndex = 9999999,
    zoneAxis = 'x',
    color = 'blue'          
    
  ) %>%

  hc_subtitle(text = "") %>%
  hc_legend(enabled = FALSE) %>%
  hc_xAxis(enabled = FALSE,title = FALSE) %>%
  hc_yAxis(enabled = FALSE,title = FALSE,
           labels = list(enabled = FALSE)) %>%
  
  hc_plotOptions(
    column = list(stacking = NULL,pointWidth = 10)
    
  )

我试图了解如何更改列的位置。

kqlmhetl

kqlmhetl1#

是的,这是可能的,但我不相信它可以用hchart来完成。因为你需要对每个系列有不同的设置。你需要使用hc_add_series来将每个单独的系列添加到绘图中。然后将groupPadding设置为.5,并调整每个系列的pointWidth。

library(dplyr)
library(gapminder)
library(highcharter)

dat = gapminder::gapminder %>%
  filter(year >= 1990,country %in% c('Chile','Argentina')) %>%
  mutate(year = as.factor(year))

highchart(type = "chart") %>%
  hc_xAxis(type = "category") %>%
  hc_add_series(dat[dat$country == "Chile",], hcaes(x = year, y = gdpPercap), type = "column",
                groupPadding = 0.5, pointWidth = 80, name = "Chile") %>%
  hc_add_series(dat[dat$country == "Argentina",], hcaes(x = year, y = gdpPercap), type = "column",
                groupPadding = 0.5, pointWidth = 40, name = "Argentina") %>%
  hc_tooltip(shared = TRUE)

相关问题