R绘图:具有累积值的堆积面积图不能正确堆积

时间:2019-03-11 09:49:37

标签: r plotly

我想在10天的采样期内绘制三列数据作为累积堆积面积图。

ID变量值

达拉斯样品.01 0.0012

奥斯汀样品。010.23

西雅图样品。010.01

我希望它看起来像这样:

enter image description here

但是它像这样出来:

enter image description here

我的代码在做什么错?

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)

2 个答案:

答案 0 :(得分:0)

我认为您需要逐个跟踪添加组跟踪。如以下示例(来自here):

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

答案 1 :(得分:0)

如果您是从积蓄地跟踪累积示例,请不要执行group_by .... cumsum,我也将摆脱fillcolor