0

I am trying to make a contour plot and draw a line on top of it (which I can do). I then overlaid two other lines using add_trace. For some reason the two lines I add using add_trace comes out orange instead of grey even though I specify grey (line = list(color = 'grey', )

I don't know how to add my data, it is very big. Is there any obvious reason as to why the colour changes to orange? If I change the width or the dash, it works. it just doesn't want to use the grey colour!

Thank you

 (plot <- plot_ly(df, x = ~A, y = ~B, z = ~Difference, zauto = FALSE, zmin = -250, zmax = 250,
                                          type="contour",  
                                          colorbar = list(title = "", titleside='right',
                                                          tickvals=c(-250, -200, -150, -100, -50, 0, 50, 100, 150, 200, 250), len = 1),
                                          colorscale = "RdBu", 
                                          contours = list(start = 0, end = 0, coloring='heatmap', coloring='lines'),
                                          line = list(color = 'black', width = 2)) %>%
   add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'dash'), contours = list(start = 0, end = 0, coloring='lines')) %>%
   add_trace(z = df$C, showscale = FALSE, line = list(color = 'grey', width = 2, dash = 'solid'), contours = list(start = 0, end = 0, coloring='lines')) %>%
   layout(margin = list(l = 50, r = 70, b = 50, t = 50, pad = 4),
     title = "", xaxis = x, yaxis = list(title = ""), font=t))
3
  • You can use dput(head(df, 100)) to share your data - or create a dummy data.frame. Commented Aug 21, 2020 at 11:01
  • The limit is 30 thousand characters and if I use put(head(df, 50)) I have almost 70 thousand characters. Can I link it via Dropbox or the likes? Commented Aug 21, 2020 at 11:19
  • Please see my answer below - just try to use dummy data if your actual data isn't suitable. Commented Aug 21, 2020 at 12:01

1 Answer 1

1

I guess the problem in your above code is, that you aren't specifiying the trace type. Accordingly plot_ly assumes that you are adding two more contour traces. Those traces are inheriting the colorscale you defined.

To avoid this you need to specify type = "scatter", mode = "lines", inherit = FALSE.

I made a simple example based on this.

library(plotly)

fig <- plot_ly(
  x = c(-9, -6, -5, -3, -1), 
  y = c(0, 1, 4, 5, 7), 
  z = matrix(c(10, 10.625, 12.5, 15.625, 20, 5.625, 6.25, 8.125, 11.25, 15.625, 2.5, 3.125, 5, 8.125, 12.5, 0.625, 1.25, 3.125,
               6.25, 10.625, 0, 0.625, 2.5, 5.625, 10), nrow = 5, ncol = 5), 
  type = "contour", colorbar = list(title = "", titleside='right'),
  colorscale = "RdBu", 
  line = list(color = 'black', width = 2)) %>% 
  add_trace(x = -1:-7, y = 1:7, type = "scatter", mode = "lines", line = list(color = 'lightgreen', width = 2, dash = 'solid'), inherit = FALSE)

fig

result

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, and thank you for the dummy data too. I tried with my data and the two orange lines disappeared. I wonder if its because the two that I am trying to add_trace are also contour plots. I just do not want the background colour scheme of the contour plots and instead I want the only want one line plot. I have it like this now and both the orange lines disappeared. add_trace(z = fixedPAR$Diff_growth, showscale = FALSE, type = "scatter", mode = "lines", line = list(color = 'grey', width = 2, dash = 'dash'), contours = list(start = 0, end = 0), inherit = FALSE ) %>%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.