在rCharts中删除yAxis上的小数

时间:2019-06-06 10:08:36

标签: plot shiny rcharts yaxis

我正在使用rCharts在闪亮的仪表板上绘制nPlot。.在yAxis上,我有大数字(9位数字)加1个小数(零),请查看此屏幕截图yAxis labels 我想摆脱零(突出显示的黄色),我尝试了一些在stackoverflow上发现的解决方案,但到目前为止没有任何作用

我尝试将format(round())用于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)

})

并收到此错误: 数学函数的非数字参数

我试图在rCharts中使用TickFormat

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

得到他的结果yAxis with tickFormat,所有逗号都被删除,但仍然与yAxis线重叠

我也尝试添加一些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;
}

结果:在此屏幕快照output with CSS中 也不是很好,因为数字超出了框的边界!

我也尝试过更改框的边框大小,但这没有帮助

请帮忙吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

您可以使用p$chart(margin = list(left = 100))设置左边距,也可以通过执行p$yAxistickPadding = 15中设置填充。

所需的数字格式设置为tickFormat = "#! function(d) {return d3.format('c')(8364) + d3.format(',.1')(d)} !#"(8364是欧元符号的十进制代码)。

所以:

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