R绘图返回空白森伯斯特图

时间:2019-09-26 22:27:47

标签: r plotly sunburst-diagram

我正在尝试使用plotly创建朝阳图,但是它返回一个空白图。我的数据集包含的货币信息与下面类似(除了更多行):

df <- data.frame(
MainGroup = c("A", "A","A"),
Currency = c("USD","EUR","GBP"),
Percent = c(90,5,5),
Category = c("USD","Other","Other"),
stringsAsFactors = FALSE)

Category基于Percent列:小于10的任何内容都将归类为“其他”。我尝试了plot_ly(df, labels = ~Currency, parents = ~Category, values = ~Percent, type = 'sunburst'),但返回了空白图表。我想创建一个像这样的图表:

enter image description here

1 个答案:

答案 0 :(得分:0)

df <- data.frame(
        MainGroup = c("A", "A","A"),
        Currency = c("USDollars","EUR","GBP"),
        Percent = c(90,5,5),
        Category = c("USD","Other","Other"),
        stringsAsFactors = FALSE)    

plot_ly(labels = c(" ", "USD","Other",df$Currency), parents = c(""," "," ",df$Category), values = c(100,90,10,df$Percent), type = 'sunburst',
    branchvalues='total')

这里有多个问题(我不得不说这些朝阳图很棘手):

  • 您必须为每个元素提供父母,在您的示例中Other没有父母
  • 您有两个不同的项目,称为USD,这是指定父母的问题,我将其中一个更改为USDollars
  • 要获得整个圈子,您必须输入branchvalues='total'

image