rplotshinyrchartsyaxis

Removing decimals on yAxis in rCharts shiny


I am plotting an nPlot using rCharts in shiny dashboard.. on the yAxis, i have big numbers (9 digits) plus 1 decimal (zero), please see this screenshot yAxis labels I want to get rid of the zero (highlighted yellow), I have tried several solutions i found on stackoverflow, but nothing has worked till now

I tried to use format(round()) for the variable that is plotted in the yAxis

   ct$Market = as.character(ct$Market)
  output$top10markets <-renderChart({
    topmarkets <- 
      arrange(ct %>%  
                group_by(as.character(Market)) %>% 
                summarise(
                  CTo = format(round(sum(`Net turnover`)), digits = 0)
                ), desc(CTo))
    colnames(topmarkets)[colnames(topmarkets)=="as.character(Market)"] <- "Market"

    topmarkets <- subset(topmarkets[1:10,], select = c(Market, CTo))
    topmarkets$CTo <- format(round(topmarkets$CTo, digits = 0))

    p <- nPlot(CTo~Market, data = topmarkets, type = "discreteBarChart", dom = "top10markets") 
    p$params$width <- 1000
    p$params$height <- 200
    p$xAxis(staggerLabels = TRUE)
    p$yAxis(staggerLabels = TRUE, width = 10)

return(p)

})

and got this Error:non-numeric argument to mathematical function

I tried to use the TickFormat inside rCharts

p$yAxis(staggerLabels = TRUE, width = 50, tickFormat = "#! function(d) {return '€' + d} !#")

and got his result yAxis with tickFormat all commas are removed and still it overlaps the yAxis line

i tried also to add some CSS:

.nv-discreteBarWithAxes .nvd3 > g > g > text,
.nv-axisMaxMin text {
  transform: translateX(13px); 
  width: 150px;
  height: 80px;
  -ms-transform: rotate(20deg); 
  -webkit-transform: rotate(20deg);
  transform: rotate(20deg); 
}
.nv-axisMaxMin text {
  word-break: break-word;
}

Result: in this screenshot output with CSS also not good as numbers are exceeding the box borders!

I have tried also to change the box border sizes but it didn't help

Please any help?

thanks a lot


Solution

  • You can set the left margin with p$chart(margin = list(left = 100)), and you can set a padding in p$yAxis by doing tickPadding = 15.

    The number formatter you want is tickFormat = "#! function(d) {return d3.format('c')(8364) + d3.format(',.1')(d)} !#" (8364 is the decimal code of the euro sign).

    So:

    library(rCharts)
    
    dat <- data.frame(
      Market = c("A", "B", "C"),
      CTo = c(1000000, 5000000, 10000000)
    )
    
    p <- nPlot(CTo~Market, data = dat, type = "discreteBarChart")
    p$yAxis(tickPadding = 15, tickFormat = "#! function(d) {return d3.format('c')(8364) + d3.format(',.1')(d)} !#")
    p$chart(margin = list(left = 100))
    p
    

    enter image description here