Search code examples
rplotgraphhighchartsr-highcharter

Highchart: Can I use a different variable as the data labels?


I'm trying to build a column chart through highchart in r studio. I've converted the values to % as I want the graph to show %, but I want the data labels to show the value, is there a way of doing this?

My data set has a column with the values for London and the percentages for London, I want the Y axis of the graph to show the % while the data labels show the value.

This is my current code:

    hc <- highchart() %>%
  hc_title(text= "Gender - London")%>%
  hc_colors('#71599b') %>%
  hc_yAxis(max = 0.7) %>%
  hc_xAxis(categories = Sex$Gender) %>%
  hc_add_series(name = "London", type = "column",
                data = Sex$LON_PERC, dataLabels = list(enabled=TRUE, format={Sex$London}) ) 

So, I've put Sex$LON_PERC (% in London) as the data to plot while Sex$London is the data labels.

But this code puts all the values of London in each data label.

enter image description here

Edit:

This is the data I'm trying to plot, LON_PERC on the Y Axis, Gender on the X axis and London as the Data Labels

Gender    London    LON_PERC
Declined    5      0.000351247
Female    8230     0.578152441
Male      4640     0.325957148
No Data   1360     0.095539164

Solution

  • I've found a work around.

    So, I can add in a "tooltip" that appears when I hover over the column/bar.

    Firstly, a function is needed:

    myhc_add_series_labels_values <- function (hc, labels, values, text, colors= NULL, ...) 
    {
      assertthat::assert_that(is.highchart(hc), is.numeric(values), 
                              length(labels) == length(values))
      df <- dplyr::data_frame(name = labels, y = values, text=text)
      if (!is.null(colors)) {
        assert_that(length(labels) == length(colors))
        df <- mutate(df, color = colors)
      }
      ds <- list_parse(df)
      hc <- hc %>% hc_add_series(data = ds, ...)
      hc
    }
    

    and then when creating the highchart this function needs to be called.

    The data looks as follows:

    Sex <- read.table(header = TRUE, text = 
                "Gender     London     LON_PERC
                 Declined       5      0.000351247
                 Female      8230      0.578152441
                 Male        4640      0.325957148
                'No Data'    1360      0.095539164
                      ")
    

    Then the code to generate the highchart is:

    Gender<- highchart() %>%
          hc_xAxis(categories = Sex$Gender, labels=list(rotation=0))%>%
          myhc_add_series_labels_values(labels = Sex$Gender,values=Sex$LON_PERC, text=Sex$London, type="column")%>%
          hc_tooltip(crosshairs=TRUE, borderWidth=5, sort=TRUE, shared=TRUE, table=TRUE,pointFormat=paste('<br>%: {point.y}%<br>#: {point.text}'))%>%
          hc_legend()
    

    This gives the below output:

    enter image description here

    Then when I hover over each column/bar it gives be the % information and the number information as can be seen here:

    enter image description here