r - Highcharter 中的手动轴标签

标签 r highcharts visualization r-highcharter

由于 highcharts 不提供人口金字塔函数,因此我通过为金字塔一侧的数据分配负值,使用柱形图类型手动捏造了一个函数。我面临的唯一问题是我无法弄清楚如何覆盖 x 轴标签,以便我可以将这些值显示为正值。

Image of population pyramid highlighting the issue

我还操纵了标题和副标题函数,以便我可以为金字塔的两侧创建标题。

这里是一些示例代码:

data <- data.frame(x=as.factor(c(seq(1,10,1), seq(1,10,1))), 
                   y=sample(10:20, size=20, replace=T), 
                   group=c(rep(1,10), rep(2,10)))
data <- data %>%
  mutate(y = ifelse(group == 1, (-1)*y, y))

highchart() %>%
  hc_add_series(
    data,
    type = "bar",
    hcaes(
      x = x,
      y = y,
      group = group
    ), showInLegend = F) %>% 
  hc_plotOptions(bar = list(stacking = "normal")) %>% 
  hc_title(text = "Group 1", align= "center", x = -115, y=20, margin=0,
           style = list(fontSize="12px", color="#0d233a")) %>%
  hc_subtitle(text = "Group 2", align= "center", y=20, margin=0,
              x = 130, style = list(fontSize="12px", color="#2f7ed8"))

我的主要问题是如何手动覆盖 x 轴,以便可以为金字塔的左侧分配正轴标签。此外,如果有人知道如何以编程方式将标题和副标题居中,使它们位于金字塔每一侧的中心,那就太好了。

我尝试将 x 轴设为分类并使用“类别”选项来分配标签,但不知何故显示 20 代替 0,我不知道为什么或如何修复它。

ylim <- plyr::round_any(max(abs(data$y), na.rm=T), 5)
highchart() %>%
  hc_add_series(
    data,
    type = "bar",
    hcaes(
      x = x,
      y = y,
      group = group
    ), showInLegend = F) %>% 
  hc_plotOptions(bar = list(stacking = "normal")) %>% 
  hc_title(text = "Group 1", align= "center", x = -115, y=20, margin=0,
           style = list(fontSize="12px", color="#0d233a")) %>%
  hc_subtitle(text = "Group 2", align= "center", y=20, margin=0,
              x = 130, style = list(fontSize="12px", color="#2f7ed8")) %>%
  hc_yAxis(type="category", categories = as.factor(c(rev(seq(0,ylim,5)),
                                                     seq(5,ylim,5))), tickInterval=5)

我非常感谢您的帮助!我也愿意采用一种完全不同的方法来使用 highcharter 包生成人口金字塔。

最佳答案

由于 x 轴上有值,Highcharts 认为 y 是 x,x 是 y。 (我花了一分钟才弄清楚为什么它没有听我说话。)

在 R 代码中,您将看到对 hc_yAxis() 的调用。 formatter 函数会将您的 x 轴标签调整为其绝对值。

highchart() %>%
  hc_add_series(data, type = "bar", hcaes(x = x, y = y, group = group), 
                showInLegend = F) %>% 
  hc_plotOptions(bar = list(stacking = "normal")) %>% 
                        # format the labels on the x-axis (y-axis per HC)
  hc_yAxis(labels = list(formatter = htmlwidgets::JS( 
    "function() {
        return Math.abs(this.value); /* all labels to absolute values */
    }"
  ))) %>% 
  hc_title(text = "Group 1", align = "center", x = -115, y = 20, margin = 0,
           style = list(fontSize = "12px", color = "#0d233a")) %>%
  hc_subtitle(text = "Group 2", align = "center", y = 20, margin = 0,
              x = 130, style = list(fontSize = "12px", color = "#2f7ed8"))

enter image description here

关于r - Highcharter 中的手动轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75077406/

相关文章:

R - 管道 ("pbcopy")列未与粘贴对齐

r - 根据 data.frame 的一对或多对值创建子集函数

java - Highcharts 和 Java : saving image on server side

javascript - 为什么两次应用相同的数据会破坏图表?

3d - Paraview:查看 3D 对象的 2D 投影

python - Bokeh 中带有对数轴的四边形

r - 使用 ggplot2 在 R 上创建地平线图 : show percentage change

r - 如何使用带有多参数函数的magrittr管道?

r - auto.arima 未并行化

javascript - 如何使用 jQuery 选择路径标记