Hi,
I’m trying to plot a raster image onto a lat/lon coordinate plane. The ggplot output looks like this:
However, when I attempt to turn this plot into a plotly object, I get the error message “Warning message:
’heatmap’ objects don’t have these attributes: ‘mode’”. My plotting code is as follows:
r_spdf <- as(d, "SpatialPixelsDataFrame") # turn raster to SPDF
r_df <- as.data.frame(r_spdf)
names(r_df)[1] <- "Thermal Habitat" #Change column name for legend title
p <- ggplot() +
geom_polygon(data=boundary, aes(x=long, y=lat), fill="#9AC5E3") + #Ocean
geom_sf(data = ne_countries, fill = "white",color = "white", size = 0.25) + #USA
geom_sf(data = ne_states, fill = "white",color = "white", size = 0.05) + #States
geom_tile(data=r_df, aes(x=x, y=y, fill=`Thermal Habitat`), alpha=0.8) + #Raster layer
scale_fill_gradient2(low = "blue",mid = "white",high = "red",midpoint = 0.5) +#Color
coord_sf(crs = crs, xlim = xlims, ylim = ylims) + #Coordinate limits
theme_map() +
theme(legend.text = element_blank(),
plot.background=element_blank(),
panel.border=element_blank())
plt <- ggplotly(p) %>%
layout(
title = "Black Sea Bass Thermal Habitat",
titlefont = list(size = 15),
margin = list(b = 50, t = 50),
autosize = F,
width = 500,
legend = list(x = 0.1, y = 0.9,
bgcolor = "#E2E2E2",
bordercolor = "#FFFFFF",
borderwidth = 2)
)
After doing some reading, I found that I need to turn the raster layer into a matrix before incorporating with plotly, but the problem is that I lose also lat/lon data when I do so. How can I keep lat/lon information to turn the above figure into a plotly compatible heatmap?