I’m trying to get some text displayed when people hover over an arbitrary rectangle.
I thought this would work … but it doesn’t.
library(tidyverse)
library(plotly)
getPalette = colorRampPalette(brewer.pal(9, "OrRd"))
df<-tribble(
~Region,~Population,~PerCapEnergy,~xmin,~xmax,~ymin,~ymax,
"North America",377,1492,0,377,0,1492,
"Europe",640,779,377,1018,0,779,
"China",1410,629,1018,2429,0,629
)
fig<-plot_ly()
l<-list()
pdf<-df %>% mutate(colr=rev(getPalette(nrow(df))))
l<-pdf %>% pmap(
function(...) {
c<-tibble(...)
l<-append(l,list(type="rect",text=c$Region,hoverinfo="text",x0=c$xmin,x1=c$xmax,y0=c$ymin,y1=c$ymax,fillcolor=c$colr,line=list(width=0)))
}
)
layout(fig,title="TITLE", shapes=l)