如何用R语言的Highcharts库将饼图转换为环形图?
将Highcharts饼图转为环形图的方法
只需在hc_add_series()中添加innerSize参数,即可将饼图转换为环形图。该参数用于设置饼图中心空白区域的尺寸,通常以百分比为单位。
修改后的完整代码
highchart() %>% hc_add_series(type = "pie", data = age, hcaes(Age, Mean_percentage_viewed), size = "60%", innerSize = "40%", # 新增innerSize定义环形内半径 name = "Mean Percentage viewed", center = c(50, 50), dataLabels = list(distance = -50, formatter = JS("function () { return this.y > 5 ? this.point.name : null; }"))) %>% hc_title(text = "Age group share")
参数说明
innerSize = "40%":控制环形中心空白区域的大小,数值越小环形越宽,数值越大环形越窄,可根据视觉需求调整百分比。- 其余参数保持原有设置即可,数据标签、标题等交互功能不受影响。
你提供的数据集
age <- structure(list(Age = c("13–17 years", "18–24 years", "25–34 years", "35–44 years", "45–54 years", "55–64 years"), Views = c(0.38, 70.01, 20.2, 6.91, 2.37, 0.13), Mean_view_duration = c("0:01:51", "0:02:25", "0:02:42", "0:02:58", "0:02:57", "0:02:23"), Mean_percentage_viewed = c(21.71, 21.23, 24.31, 26.36, 24.94, 19.27), Watch.Time.Hours = c(0.28, 67.14, 21.58, 8.11, 2.76, 0.12)), class = "data.frame", row.names = c(NA, -6L))
内容的提问来源于stack exchange,提问作者Ranji Raj
相关产品推荐
相关产品推荐

