r - 绘制两条曲线之间的阴影区域

标签 r plotly

我刚刚做了一个线图,我想对第一条曲线和第三条曲线之间的区域进行着色。

我正在使用 plotly,那么我可以使用哪个函数?一个多边形?

这是我的完整代码,没有我不成功的尝试。

谢谢 !

library(ggplot2)
trace1 <- list(
  x = c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013), 
  y = c(100, 101.6674, 103.1031, 103.0395, 104.6586, 105.9702, 106.929, 107.8877, 109.2521, 110.298, 111.2798, 112.4942, 113.746, 114.8573, 114.931, 116.6362, 117.4891, 118.1476, 117.9527), 
  line = list(color = "rgb(44, 160, 44)"), 
  mode = "lines", 
  name = "Real median compensation", 
  type = "scatter",
)
trace2 <- list(
  x = c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013), 
  y = c(100, 101.9157, 103.0659, 104.1, 106.134, 107.7038, 109.2065, 110.2957, 111.7355, 112.9352, 114.1363, 115.8782, 117.7302, 118.5249, 119.0169, 120.3521, 121.5813, 122.2877, 122.123), 
  line = list(color = "rgb(31, 119, 180)"), 
  mode = "lines", 
  name = "Real average compensation", 
  type = "scatter", 
)

trace3 <- list(
  x = c(1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013), 
  y = c(100, 101.6726, 103.6097, 104.7142, 107.2813, 110.2709, 111.7217, 113.2652, 115.0878, 118.0551, 119.7772, 122.3167, 124.2049, 124.0089, 122.0517, 125.6065, 127.3615, 127.3349, 127.5306), 
  connectgaps = TRUE, 
  line = list(color = "rgb(214, 39, 40)"), 
  mode = "lines", 
  name = "Productivity", 
  type = "scatter", 
  )
data <- list(trace1, trace2, trace3)
layout <- list(
  annotations = list(
    list(
      x = -0.05, 
      y = 1, 
      showarrow = FALSE, 
      text = "Index 1995 = 100", 
      xref = "paper", 
      yref = "paper"
    )
  ), 
  autosize = TRUE, 
  hovermode = "closest", 
  showlegend = TRUE, 
  title = "<b>Macro-level decoupling reflects declines in labour shares and increases in wage inequality</b>", 
  xaxis = list(
    autorange = TRUE, 
    range = c(1995, 2013), 
    title = "", 
    type = "linear"
  ), 
  yaxis = list(
    autorange = TRUE, 
    range = c(98.4705222222, 129.060077778), 
    title = "", 
    type = "linear"
  )
)
p <- plot_ly()
p <- add_trace(p, x=trace1$x, y=trace1$y, line=trace1$line, mode=trace1$mode, name=trace1$name, type=trace1$type, uid=trace1$uid, xsrc=trace1$xsrc, ysrc=trace1$ysrc)
p <- add_trace(p, x=trace2$x, y=trace2$y, line=trace2$line, mode=trace2$mode, name=trace2$name, type=trace2$type, uid=trace2$uid, xsrc=trace2$xsrc, ysrc=trace2$ysrc)
p <- add_trace(p, x=trace3$x, y=trace3$y, connectgaps=trace3$connectgaps, line=trace3$line, mode=trace3$mode, name=trace3$name, type=trace3$type, uid=trace3$uid, xsrc=trace3$xsrc, ysrc=trace3$ysrc)
p <- layout(p, annotations=layout$annotations, autosize=layout$autosize, hovermode=layout$hovermode, showlegend=layout$showlegend, title=layout$title, xaxis=layout$xaxis, yaxis=layout$yaxis)
p

最佳答案

这是您的问题的解决方案。

# Add the first line that define the shaded area using plot_ly
p <- plot_ly(x=trace1$x, y=trace1$y, line=trace1$line, mode=trace1$mode,
       name=trace1$name, type=trace1$type, uid=trace1$uid, xsrc=trace1$xsrc,
       ysrc=trace1$ysrc)

# Add the second line that define the shaded area using add_trace
# Set the fill and fillcolor options
p <- add_trace(p, x=trace3$x, y=trace3$y, connectgaps=trace3$connectgaps,
       line=trace3$line, mode=trace3$mode, name=trace3$name, type=trace3$type,
       uid=trace3$uid, xsrc=trace3$xsrc, ysrc=trace3$ysrc,
       fillcolor="rgba(0,40,100,0.2)", fill = 'tonexty')

# The third line
p <- add_trace(p, x=trace2$x, y=trace2$y, line=trace2$line, mode=trace2$mode, 
       name=trace2$name, type=trace2$type, uid=trace2$uid, xsrc=trace2$xsrc, 
       ysrc=trace2$ysrc)   
p

enter image description here

关于r - 绘制两条曲线之间的阴影区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695999/

相关文章:

python - Plotly:如何将水平滚动条添加到 plotly 表达图形?

r - 使用 data.table 在第一次匹配时进行高效的字符串分割

r - 向量化插入元素

r - 在 R 中使用粘贴

python - Plotly:如何创建具有多色标签的时间序列变量的线图?

javascript - Plotly.js 自动缩放

r - 添加多图轴

r - 创建一个从开始计数到当前值的列表

python - 本地 CSS 文件从 plotly dash 应用程序中掉落

python - 颜色条不遵守限制值