绘图中具有两种颜色的面积图

时间:2018-10-15 20:43:12

标签: r plotly

我正在尝试使用r创建一个绘图区域图。到目前为止,我得到的是两种颜色。我有两个区域,一个是实际区域,另一个是模型值,因此,如果模型区域超出实际区域,则应该显示红色,如果下降,则应该为绿色。 (例如:https://bl.ocks.org/mbostock/raw/3894205/

下面是我的代码

library(dplyr)
library(plotly
        )
ab <-tibble::tribble(
  ~modeled, ~actuals, ~weekenddate,  ~err,
  501384,   481864, "2014-02-02", 19519,
  488933,   479078, "2014-02-09",  9856,
  484191,   464026, "2014-02-16", 20165,
  480443,   460339, "2014-02-23", 20104,
  482512,   464021, "2014-03-02", 18491,
  488843,   462458, "2014-03-09", 26385,
  481864,   491864, "2014-04-02", 19519,
  481864,   501864, "2014-04-09",  9856,
  464026,   480443, "2014-04-16", 20165,
  460339,   484191, "2014-04-23", 20104,
  464021,   488933, "2014-05-02", 18491,
  464021,   501384, "2014-05-09", 26385,
  501384,   481864, "2014-06-02", 19519,
  488933,   479078, "2014-06-09",  9856,
  484191,   464026, "2014-06-16", 20165,
  480443,   460339, "2014-06-23", 20104,
  482512,   464021, "2014-07-02", 18491,
  488843,   462458, "2014-07-09", 26385,
)

diff_charts <- plot_ly(x = ~ab$weekenddate, y = ~ab$modeled, type = 'scatter', mode = 'none', name = 'Modeled', fill = 'tozeroy',
                       fillcolor = 'rgba(255,0,0, 0.5)') %>%
  add_trace(x = ~ab$weekenddate, y = ~ab$actuals, name = 'Actuals', fill = 'tozeroy',
            fillcolor = 'rgba(0,255,0, 0.5)') %>%
  layout(xaxis = list(title = 'Carat'),
         yaxis = list(title = 'Density'))

diff_charts

我还应该如何在一定范围后获得图表以清楚地看到变化。

0 个答案:

没有答案