1

enter image description hereI would like to have a contour plot that shows where the ratio of the response is 1. x axis is saturation, y axis is temperature and z is the ratio of spA1 to spA2

I can do this in ggplot2 but all my other figures are in plotly and I would like to be able to eventually overlay other lines and have a secondary axis which plotly does really well.

can this be done in plotly?

I have tried this in ggplot 2 using geom_raster and geom_contour(breaks=1) and it works but I tried in plotly using add_trace(z=1, type = "scatter", mode = "line")) and it just draws weird lines everywhere...

Data: https://www.dropbox.com/s/gjdr5uuys6tqswr/df.csv?dl=0

Ratio <-(spA1/spA2)

#this works
Ratio <- ggplot(Ratio, aes(x = Saturation, y = Temp, z =    
Ratio, fill = Ratio)) +  geom_raster(interpolate = T) +   
geom_contour(breaks = 1, colour="black", size=1) + theme(plot.title 
= element_text(size = 12)) + 
scale_fill_gradientn(colors=c("red","white","blue"),      
values=rescale(c(0,1, 3)))

#this doesn't work
Ratio <- plot_ly(Ratio, x = ~ Saturation, y = ~ Temp, z =    
~Ratio, type="contour", colorbar = list(title = "Ratio"),
            colorscale = list(
              c(0,1,2),
              c("red", "white", "blue"))) %>%
 add_trace(z=1, type = "scatter", mode = "line")

Is it possible to do this in plotly? I would like to do a few of these lines and overlay them in plotly with a secondary axis. Thank you in advance!

3
  • Could you include a figure of your ggplot version? Commented May 6, 2019 at 8:52
  • 1
    Hi @vestland I have just included a figure from ggplot. Its using a similar dataset and the black line represents the 1:1 from the 'Ratio' calculated... I have a few of these lines but because I am looking at different factors, I would like to be able to use plotly and have the different axis with different scales so I can merge them into one plot.. thank you for your help! Commented May 6, 2019 at 11:53
  • I managed to draw the 1:1 line but I can't get the diverge colour gradient to work.... does plotly do diverge colour palette? (ratio <- plot_ly(Ratio, x = ~Saturation, y = ~Temp, z = ~Ratio, type="contour", colorbar = list(title = "Ratio"), colorscale = "rbu", contours = list( start = 1, end = 1))) Commented May 6, 2019 at 20:07

1 Answer 1

2

I figured it out. It's

colorscale = "RdBu", contours = list( start = 1, end = 1, coloring='heatmap',    
coloring='lines'), line = list(color = 'black', width = 2)))
Sign up to request clarification or add additional context in comments.

1 Comment

Great! This is what I was looking for. Any way to plot more than one contour line (start, end) using a vector with more than 1 value? Instead of only 1 as your case.

Your Answer

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