在R中使用Highcharter在Highcharts饼图上显示标签名称和数值。

huangapple go评论71阅读模式
英文:

Displaying Label Name and Value on Highcharts Pie Chart in R using Highcharter

问题

我想在我的R中的highcharter饼图上同时显示点名称和百分比值。我设法显示了百分比值,但它取代了名称值,而不是补充它。这里是一个代码示例:

library(tidyverse)
library(highcharter)

df = tibble(
  id = c(1,2,3,4,5),
  name = c('John','Kenneth','Aida','Ronda','Jasmine'),
  value = c(0.2,0.35,0.1,0.3,0.05)
)

df %>% 
  hchart('pie',
         hcaes(name, value))

在这里,我获得了百分比值,但它们取代了名称值:

df %>% 
  hchart('pie',
         hcaes(name, value)) %>% 
  hc_plotOptions(pie = list(
    dataLabels = list(
      enabled = TRUE,
      format = '{point.percentage:.1f} %'
    )
  ))

有没有办法同时显示两者。这些值可以位于饼图片段内。

英文:

I would like to display both the point name and percentage value on my highcharter pie chart in R. I managed to display the percentage value, but it replaced the name value instead of complementing it. Here's a code sample:

library(tidyverse)
library(highcharter)

df = tibble(
  id = c(1,2,3,4,5),
  name = c('John','Kenneth','Aida','Ronda','Jasmine'),
  value = c(0.2,0.35,0.1,0.3,0.05)
)

df %>% 
  hchart('pie',
         hcaes(name, value))

Here I got the percentage values, but they replaced the name values:

df %>% 
  hchart('pie',
         hcaes(name, value)) %>% 
  hc_plotOptions(pie = list(
    dataLabels = list(
      enabled = TRUE,
      format = '{point.percentage:.1f} %'
    )
  ))


Any idea how to get both displayed. The values can be inside the pie slices.

答案1

得分: 2

你可以使用 {point.name} 来显示标签,就像这样:

library(tidyverse)
library(highcharter)

df %>% 
  hchart('pie',
         hcaes(name, value)) %>% 
  hc_plotOptions(pie = list(
    dataLabels = list(
      enabled = TRUE,
      format = '{point.name} ({point.percentage:.1f} %)'
    )
  ))

在R中使用Highcharter在Highcharts饼图上显示标签名称和数值。

创建于2023年03月03日,使用 reprex v2.0.2

英文:

You could use {point.name} to show the label like this:

library(tidyverse)
library(highcharter)

df %>% 
  hchart('pie',
         hcaes(name, value)) %>% 
  hc_plotOptions(pie = list(
    dataLabels = list(
      enabled = TRUE,
      format = '{point.name} ({point.percentage:.1f} %)'
    )
  ))

在R中使用Highcharter在Highcharts饼图上显示标签名称和数值。

<sup>Created on 2023-03-03 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月3日 19:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定