plotly R API,是否可以为饼图提供子图?

时间:2016-03-09 21:02:08

标签: r plotly

我希望用plotly的R API绘制两个饼图。但是,它似乎不起作用。

library(plotly) 
ds <- data.frame(labels = c("A", "B", "C"),
             val1 = c(10, 40, 60),
             val2 = c(20,40, 50))

 p1 <- plot_ly(ds, labels = labels, values = val1, type = "pie") 
 p2 <- plot_ly(ds, labels = labels, values = val2, type = "pie") 

 subplot(p1, p2, margin=0.05)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我会尝试回答。对于使用plot_ly()的饼图,layout()中使用的常规参数不起作用。您需要在plot_ly()add_trace()调用中指定域名,如下所示:

library(plotly)

df <- data.frame(labels1 = LETTERS[1:5], values1 = sample(100:200, size = 5),
                 labels2 = LETTERS[6:10], values2 = sample(100:200, size = 5))

plot_ly(df, labels = labels1, values = values1, type = "pie", 
        domain = list(x = c(0, 0.4)), showlegend = F) %>% 

  add_trace(labels = labels2, values = values2, type = "pie", 
            domain = list(x = c(0.6, 1)), showlegend = F) %>% 

  layout(title = "Pie chart - subplot")

希望这会有所帮助......