I'd like to overlay a line on top a Plotly contour plot, similar to overlaying a line on an image of a matrix in which the intensity represents position in z
within R3
:
# Generate an arbitrary matrix
m <- matrix(sin(1:6^2) * 1:6, nrow = 6)
# Define a path
path <- data.frame(x = c(0:7), y = c(0, 1, 2, 2, 3, 3, 4, 6))
image(x = 1:6, y = 1:6, z = m, col = gray.colors(20), xlab = "x", ylab = "y")
lines(path$x, path$y)
Which renders:
Using Plotly, I have attempted
library(plotly)
plot_ly(x = 1:6, y = 1:6, z = t(m), type = "contour") %>%
add_lines(x = path$x, y = path$y)
Which generates a contour plot overlaid with a wireframe of empty R3
space rather than a line:
You could try this:
plot_ly(x = 1:6, y = 1:6, z = t(m), type = "contour") %>%
add_trace(x = c(1, 2, 3, 4, 5, 6), y = c(1, 2, 3, 3, 4, 5), type = "scatter", mode = "line")
It gives you almost what you want. Hope it helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With