【问题标题】:highcharter both nominal and percentage valueshighcharter 标称值和百分比值
【发布时间】:2017-06-21 15:21:08
【问题描述】:

我用 highcharter 库制作了一个饼图。

library(highcharter)

test_data
  Gender  Freq  colors
1 Female 29813 #ff99cc
2   Male 38474 #2980b9

hchart(test_data, "pie", hcaes(x = Gender, y = Freq, color=colors))

由于地图是交互式的,我希望饼图在同一图上显示百分比值和标称值

有什么想法可以做到这一点吗?

【问题讨论】:

    标签: r highcharts pie-chart


    【解决方案1】:

    您需要在工具提示选项中使用带有 JS() 函数的格式化程序。此外,Highchart 使用this.point.percentage 提供百分比。确保不要忘记第一行末尾的%>%

    这应该对你有用:

    hchart(test_data, "pie", hcaes(x = Gender, label=Gender,y = Freq, color=colors))%>%
    

    hc_tooltip(formatter = JS("function(){ return '<b>' + this.point.label + ': </b>( Frequency:' +this.y+', Percentage: '+Highcharts.numberFormat(this.percentage)+'%)' }"),useHTML = FALSE)

    通过添加该行(在该行之前或之后再次需要 magrittr '%>%'): hc_plotOptions(pie =list(dataLabels = list(enabled = TRUE,format="{point.label}:{point.y}")))

    您可以在标签上添加值或将{point.y} 与以下内容交换:

    {point.percentage:.2f}%,您可以在标签(男、女)的同时添加百分比

    【讨论】:

    • 有没有办法在标签中显示名义值或百分比,而在工具提示中显示另一个?
    • @Prometheus: 是的:hc_plotOptions(pie =list(dataLabels = list(enabled = TRUE,format="{point.label}:{point.y}"))) 你可以在 point.y 和 point.percentage:.2f 之间交换以获得 %
    【解决方案2】:
    hchart(test_data, "pie", hcaes(x = Gender, y = Freq, color=colors)) %>% 
          hc_tooltip(pointFormat = "<b>Value:</b> {point.y} <br>
                     <b>Percentage</b> {point.percentage:,.2f}%")
    

    【讨论】:

    • 欢迎来到 SO。请对您的代码进行一些解释,以形成完整的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多