How to add Highchart Bar lines and lables?

Viewed 53

i would like to build an interactive chart but i'm very new in highcharts, i want to add average line for the data and change the labels of the bars, now per default say "Series 1:" i want to write "Cdays: ", this is my code now

# Load required R packages
library(highcharter) 
# Set highcharter options
options(highcharter.theme = hc_theme_smpl(tooltip = list(valueDecimals = 2)))

df <- data.frame(Year=c('2015','2016','2017','2018','2019'),
                 CD=c(24, 18, 12, 9, 14))

head(df)


hc <- df %>%
    hchart('column',
           hcaes(x = Year, y = CD),
            color = "#702080", borderColor = "#702080",
            pointWidth = 80) %>% 
    hc_title(text = "Critical Days") %>%
    hc_xAxis(categories = 'Critical Days') %>%
    
hc

Thanks !!

1 Answers

To add the mean line, try using plotLines in hc_yAxis and set the value to mean(df$CD). You can also adjust the color, add a label, etc. here.

To change the "Series 1" you see when hovering over the bars, you should set the name inside of hchart - in this case, "Cdays".

Other minor changes below - including use of df$Year for x-axis text labels.

df %>%
  hchart('column',
         hcaes(x = Year, y = CD),
         color = "#702080", 
         borderColor = "#702080",
         pointWidth = 80,
         name = "Cdays") %>% 
  hc_title(text = "Critical Days") %>%
  hc_xAxis(categories = df$Year) %>%
  hc_yAxis(
    title = list(text = "Cdays"),
    plotLines = list(
      list(
        value = mean(df$CD),
        color = "#00FF00",
        width = 3,
        zIndex = 4,
        label = list(
          text = "mean",
          style = list(color = "#00FF00")
        )
      )
    )
  )

highcharter plot with mean and Cdays text

Related