I am using R and have added data labels to a bar chart using plotly's add_text() function. However, I am wondering how I could add data labels to a filled & bordered rectangle to make them easier to see. I wasn't sure if I could do this using add_annotations(). I was hoping to render something like the data labels on this chart:
Here is an example of my current code:
my_tibble <- tibble(mins = runif(10,10,30),
week = 1:10,
exercise = c("a", "b", "b", "b", "a", "a", "b", "b", "b", "a"),
weight = runif(10,150,160))
plot_ly(data = my_tibble, hoverinfo = "none") %>%
add_trace(
type = 'bar',
x = ~week,
y = ~mins,
color = ~exercise,
hovertext = ~weight,
hovertemplate = paste('<b>Week</b>: %{x}', "%{hovertext}", '<extra></extra>')
) %>%
add_trace(
type = 'bar',
x = ~week,
y = ~mins
) %>%
layout(
barmode = 'stack'
)%>%
add_text(
x = ~week,
y = 8,
text = "label",
showlegend = F
)
CodePudding user response:
The only way I know of is to add the text as annotations, instead of a trace.
I added this to the code you have without changing anything you wrote:
layout(annotations = list(x = ~week, y = 8, showarrow = F,
bgcolor = "white", text = "label"))