problem of highchart Data Labels in Percentage

Hello everyone,

I have a problem with R Shiny highchart line chart.
The goal is to change the data labels to display percentages (%), like this: 0.1 ----> 10%.
Currently, I can only manage to append a '%' after the data label, but it is not yielding the desired result.

code:

library(shiny)
library(stringr)
library(highcharter)
library(dplyr)

#data_input
aa <- data.frame(
  year=c(2005,2006,2007,2008,2009,2010),
  age_18=c(0.951,0.804,0.758,0.68,0.60,0.582),
  age19_49=c(0.964,0.852,0.705,0.71,0.677,0.611),
  age50_74=c(0.900,0.796,0.69,0.615,0.515,0.58),
  age75_=c(0.652,0.67,0.60,0.453,0.453,0.403))

age = c("age_18")
aa_1 <- subset(aa,select=c(year,age_18)) %>%
  rename(pt=age_18) 
  aa_1$age <- age
age = c("age19_49")
aa_2 <- subset(aa,select=c(year,age19_49)) %>%
  rename(pt=age19_49) 
  aa_2$age <- age
age = c("age50_74")
aa_3 <- subset(aa,select=c(year,age50_74)) %>%
  rename(pt=age50_74) 
  aa_3$age <- age
age = c("age75_")
aa_4 <- subset(aa,select=c(year,age75_)) %>%
  rename(pt=age75_) 
  aa_4$age <- age
  
aa_all <-rbind(rbind(rbind(aa_1,aa_2),aa_3),aa_4)     
aa$age_18 <-  str_c(aa$age_18*100,"%")
aa$age19_49 <-  str_c(aa$age19_49*100,"%")
aa$age50_74 <-  str_c(aa$age50_74*100,"%")
aa$age75_ <-  str_c(aa$age75_*100,"%")

#start
ui <- fluidPage(
  fluidRow(
            column(12,
                   highchartOutput("aa_chart")))
  
)

server <- function(input,output){
  
  output$aa_table <- DT::renderDataTable(
    aa,options = list(ordering = FALSE,searching = FALSE))
  
  output$aa_chart <-renderHighchart({
    aa_all %>% 
      hchart('line', hcaes(x = year, y = pt, group=age)) %>%  
      hc_title(text = "age(%)") %>%
      hc_tooltip(pointFormat='{point.y}%')
  })
}

shinyApp(ui = ui, server = server)
#end

output:

Your assistance would be greatly appreciated! Thank you!

What if you just added the line aa_all$pt <- aa_all$pt * 100 after line 35?

Ah, I see! Thanks a lot for your help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.