3
Plot.ly對這個tutorial爲Python:堆積面積圖中Plot.ly和R
# Add original data
x=['Winter', 'Spring', 'Summer', 'Fall']
y0_org=[40, 80, 30, 10]
y1_org=[20, 10, 10, 10]
y2_org=[40, 10, 60, 80]
# Add data to create cumulative stacked values
y0_stck=y0_org
y1_stck=[y0+y1 for y0, y1 in zip(y0_org, y1_org)]
y2_stck=[y0+y1+y2 for y0, y1, y2 in zip(y0_org, y1_org, y2_org)]
[R似乎並沒有任何類似的教程。我試圖玩R的填充區繪圖教程,但未能構建堆積圖。
library(plotly)
p <- plot_ly(x = c(1, 2, 3, 4), y = c(0, 2, 3, 5), fill = "tozeroy")
add_trace(p, x = c(1, 2, 3, 4), y = c(3, 5, 1, 7), fill = "tonexty")
如何在R/Plot.ly中複製上面的代碼來創建堆積面積圖?非常感謝!
非常感謝@ Teja公司-K!代碼完美工作。然而,我的數據集並不那麼簡單 - 一些y0值可能高於y2或y1值高於y2。我將我收到的結果的屏幕截圖添加到原始帖子中。 –
@ViolaRudenko,編輯。現在應該工作。 – ytk
非常感謝!我也只是自己想出了:)你必須創建堆棧變量,從最大值開始並移動到最小值:p < - ggplot()+ geom_area(aes(x,y2 + y1 + y0 ,填充='藍色'))+ geom_area(aes(x,y1 + y0,fill ='red'))+ geom_area(aes(x,y0,fill ='green')) ggplotly –