[英]R plotly: Stacked Area Chart with Cumulative Values not stacking properly
我有三列數據,我想將其繪制為 10 天采樣期內的累積堆積面積圖。
ID變量值
達拉斯樣本.01 0.0012
奧斯汀樣品.01 0.23
西雅圖樣本.01 0.01
我希望它看起來像這樣:
但它是這樣出來的:
我的代碼做錯了什么?
melted_dat %>%
group_by(value,ID) %>%
plot_ly(
x = ~variable,
y = ~value,
color = ~ID,
type='scatter',
mode = 'none',
fill = 'tonexty',
stackgroup = 'one',
fillcolor = ~ID) %>%
layout(showlegend = FALSE)
我認為您需要逐個跟蹤添加組。 如以下示例(來自此處):
library(plotly)
data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)
p <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', stackgroup = 'one', groupnorm = 'percent', fillcolor = '#F5FF8D') %>%
add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86') %>%
add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9') %>%
add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961') %>%
add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44') %>%
layout(title = 'United States Personal Expenditures by Categories',
xaxis = list(title = "",
showgrid = FALSE),
yaxis = list(title = "Proportion from the Total Expenditures",
showgrid = FALSE,
ticksuffix = '%'))
# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = api_create(p, filename="area-stackedcum")
chart_link
如果您正在遵循 plotly 的累積示例,請不要執行group_by .... cumsum
,我也擺脫了fillcolor
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.