Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Multiple heatmaps in one plot (Useful for all kinda of raster maps)

Hello everyone,

I am trying to create an interactive map of Europe using irregular gridded orography data (Irregular grid means that lon-lat are not constant throughout each line-column of the matrix). So I thought I can use the heatmap function 3 times. The first 2 will display as hoverinfo the actual lat and lon of each grid, and the 3rd layer will be the orography.

I am recreating below a simple example. The first 2 layers of heatmap are working flawlessly, but when I add the last line I receive the error display below.

### Create data  
lon <- matrix(rnorm(100), nrow=10, ncol=10)
lat <- matrix(rnorm(100), nrow=10, ncol=10)
orog <- matrix(rnorm(100, mean=1000), nrow=10, ncol=10)
### Plot
plot_ly(z=t(lon), type = "heatmap", hoverinfo=c("z")) %>%
  add_trace(z=t(lat),  hoverinfo=c("z")) %>%

The error:
Error in p$x$data[[idx]]$marker :
$ operator is invalid for atomic vectors

Any tip would be really helpful. Thanks!

The reason why you are getting this error with the $ operator is because it is expecting a dataframe, not a vector.

See this page for more help with the operator.

Adam, thank you for the reply. I am aware of vector and data.frame structures in R. x,y,z are data.frames (well basically matrix which is kinda the same) of the same dimensions (in my example 10*10). The problem is that plotly (for some reason) limits the “overploting” of heatmap function 2 times.

Basically if I do the following it works fine, and displays as hoverinfo the values of lon and lat objects:

plot_ly(z=t(lon), type = “heatmap”, hoverinfo=c(“z”)) %>%
add_trace(z=t(lat), hoverinfo=c(“z”))

The problem is that on top of that I want to display orog. which is a 1010 data.frame too. I can understand the error, any ideas why it says I provide a vector (while clearly I provide a 1010 data.frame) in the 3rd heatmap while it works fine when overploting the 2nd heatmap?


Experiencing the same issue here (link).

1 Like

Solved my issue. Take a look, it might help.