gpt4 book ai didi

r - 如何用 highcharter 库做一个半圆 donut ?

转载 作者:行者123 更新时间:2023-12-04 12:02:49 25 4
gpt4 key购买 nike

我正在尝试使用 highcharter 库制作一个半圆形 donut ,但我只知道如何制作饼图。我知道使用 JS 可以通过添加“startAngle”和“endAngle”来实现,但我想知道如何使用 R 实现:

A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)

C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)

highchart() %>%
hc_chart(type = "pie") %>%
hc_add_series_labels_values(labels = df$A, values = df$B)%>%

hc_tooltip(crosshairs = TRUE, borderWidth = 5, sort = TRUE, shared = TRUE, table = TRUE,
pointFormat = paste('<b>{point.percentage:.1f}%</b>')
) %>%

hc_title(text = "ABC",
margin = 20,
style = list(color = "#144746", useHTML = TRUE))

enter image description here

谢谢!

最佳答案

尽管不使用 Highcharts 库,您也可以执行类似的操作。

library(tidyverse)
library(ggforce)
library(scales)
library(ggplot2)

# -------------------------------------------------------------------------

A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)

C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)

# Ensure A is a factor (we'll be using it to fill the pie)
df$A <- factor(df$A)

# compute the individual proportion in this case using var C
df$prop <- df$C/sum(df$C)

# compute the cumulative proportion and use that to plot ymax
df$p_end <- cumsum(df$prop)

# generate a y-min between 0 and 1 less value than p_end (using p_end)
df$p_start <- c(0, head(df$p_end ,-1))



# -------------------------------------------------------------------------

# plot
df %>%
mutate_at(c("p_start", "p_end"), rescale, to=pi*c(-.5,.5), from=0:1) %>%
ggplot +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = .5, r = 1, start = p_start, end = p_end, fill=A)) +
coord_fixed() +xlab("X_label") + ylab("Y_lablel") + guides(fill=guide_legend(title="Legend Title"))


输出

pit_chart

希望对您有所帮助。

关于r - 如何用 highcharter 库做一个半圆 donut ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com