英文:
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} %)'
)
))
创建于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} %)'
)
))
<sup>Created on 2023-03-03 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论