简体   繁体   中英

Manual axis labels in Highcharter

Since highcharts doesn't provide a function for population pyramids, I manually fudged one using the column plot type by assigning negative values to the data for one side of the pyramid. The only problem I am facing is that I can't figure out how to overwrite the x axis labels such that I can display these values as positive.

突出显示问题的人口金字塔图像

I also manipulated the title and subtitle functions such that I can create titles for both sides of the pyramid.

Here is some sample code:

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"))

My main question is how to manually overwrite the x axis such that the left side of the pyramid can be assigned positive axis labels. In addition, if someone has an idea about how to center the title and subtitle programmatically such that they are in the center of each side of the pyramid, that would be wonderful.

I tried to make the x axis categorical and use the "categories" option to assign labels, but somehow that displays 20 in place of 0 and I can't figure out why or how to fix it.

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)

I would really appreciate your help. I'm also open to a completely different approach for producing a population pyramid using the highcharter package.

Since you have the values on the x-axis, Highcharts perceives that the y is the x and the x is the y. (It took me a minute to figure out why it wasn't listening to me.)

In the R code, you'll see a call for hc_yAxis() . The formatter function will adjust your x-axis labels to their absolute values.

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"))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM