2016-07-26 50 views
0

我想在plotly包中使用subplot函數在單行中安排兩個餅圖,但結果圖並非我所期望的。我能夠爲折線圖等做同樣的事情,但我面臨着在單行中繪製兩個餅圖的麻煩。以下是我的代碼。在一行中的多個陰謀餅圖

ds_r <- data.frame(labels = c("Baseline", "DTC", "Detailing", "Flex"), 
       values = c(63.5, 8.5, 20.6, 7.4)) 

ds_l <- data.frame(labels = c("Baseline"), 
       values = c(100)) 

plot_right <- plot_ly(ds_r, labels = labels, values = values, type = "pie") %>% 
    layout(title = "Sales - Decomposed") 

plot_left <- plot_ly(ds_l, labels = labels, values = values, type = "pie") %>% 
    layout(title = "Total Sales") 

subplot(plot_left, plot_right, nrows = 2) 

輸出是

enter image description here

如果在圖像的底部檢查紅色方框,有兩套軸,這意味着有兩個地塊,但他們很可能得到重疊或者其他的東西。

我希望輸出是沒有任何軸的單行中的兩個餅圖。任何幫助或方向?

回答

1
library(plotly) 
ds_r <- data.frame(labels1 = c("Baseline", "DTC", "Detailing", "Flex"), 
        values1 = c(63.5, 8.5, 20.6, 7.4)) 

ds_l <- data.frame(labels2 = c("Baseline"), 
        values2 = c(100)) 
df <- cbind(ds_r, ds_l) 

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) 

我會添加圖片,但情節通常是大型文件。

+0

謝謝。這是一個好方法。將幫助我取得進展,但沒有辦法做到這一點與我認爲是更多的明星向前和一致 –

+1

我不這麼認爲。我認爲subplots主要用於對齊折線圖或散點圖,它們根據某些因素的級別進行分組。 – shayaa

1

plotly的開發版本中有新的改進的subplot功能,但看起來「錯誤」仍然存在。我不確定它與餅圖的效果如何。我在Github上提交了一個issue

# devtools::install_github('ropensci/plotly") 

library(plotly) 
ds_r <- data.frame(labels1 = c("Baseline", "DTC", "Detailing", "Flex"), 
        values1 = c(63.5, 8.5, 20.6, 7.4)) 

ds_l <- data.frame(labels2 = c("Baseline"), 
        values2 = c(100)) 

p <- plot_ly(ds_r, labels = ~labels1, values = ~values1, type = "pie", 
      showlegend = F) 

p2 <- plot_ly(ds_l,labels = ~labels2, values = ~values2, type = "pie", 
      showlegend = F) 

subplot(p, p2, nrows = 1) 

更多細節上subplot,請參閱subplot vignette