Black Lives Matter. Please consider donating to Black Girls Code today.

How to reverse the fill of a scatter plot trace?

I have the following contour graph of sea temperatures at different sea depths. I want to shade out the area beneath the max depth at each x axis point to represent the sea floor. How could I reverse the shaded portion as seen on the following image?

The code I’ve used to generate this is:

plot_ly(data = sampleData1,
             x=~factor(Station_No),
             y=~-Depth_Sample, z=~Temperature, 
             type = "contour", 
             colorscale='Jet') %>%  
  add_trace(x = ~factor(Station_No), y = ~-Depth_Max, type = "scatter", mode = "ribbon", fill='tozeroy')

Thanks!

You could introduce a helper trace at your y-min value (-13?) and use fill='tonexty'.

Here is an example:

library(plotly)
fig <- plot_ly(x = 1:12, y = -1:-12, type = "scatter", mode = "ribbon", fill='none')

add_trace(fig, x = c(1, 12), y = c(-12, -12), mode = "none", fill='tonexty',
          fillcolor = 'rgba(168, 216, 234, 0.5)', showlegend = FALSE)

image

Please also see schema(): object ► traces ► scatter ► attributes ► fill

1 Like

Thank you so much! This is the final code in case anyone else had similar issues. It stumped me initially until I realised the contour layer had to be added last.

plot_ly(data = sampleData1,
             x=~factor(Station_No),
             y=~-Depth_Max, 
             type ='scatter', mode = 'ribbon')%>%
    add_trace(x = c("BN030", "BN110"), y =c(-13.3,-13.3), mode = "none", fill='tonexty',
          fillcolor =  'darkgrey', showlegend = FALSE)%>%
    add_trace(x = ~factor(Station_No), y = ~-Depth_Sample,z=~Temperature, type = "contour", colorscale='Jet', showlegend = FALSE)