【问题标题】:plotly R API, is it possible to have subplots for pie charts?plotly R API,是否可以有饼图的子图?
【发布时间】:2016-03-09 21:02:07
【问题描述】:

我希望用 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)

感谢您的帮助。

【问题讨论】:

标签: r plotly


【解决方案1】:

我会试着回答。对于使用plot_ly() 的饼图,layout() 中使用的常规 domain 参数不起作用。您需要在 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")

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 2020-04-27
    • 2018-02-02
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多