Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R plotly highlight_key function only highlights half of the bar

Tags:

r

plotly

When I use plotly in R to plot a bar chart which has highlighting function by clicking, only half of the bar is highlighted, as can be reproduced by the following script.

library(plotly)

D = data.frame(
  NAME = LETTERS[1:5],
  VALUE = rnorm(5)
)

D %>% 
  highlight_key(~NAME) %>%
  plot_ly(x=~VALUE, y = ~NAME, type ="bar", orientation = "h") %>% 
  highlight(on = "plotly_click",off = "plotly_doubleclick")

What I can see in my Rstudio:

enter image description here

like image 731
John Avatar asked Jan 27 '26 16:01

John


1 Answers

You can solve the issue by added layout(barmode = "overlay").

library(plotly)

D = data.frame(
  NAME = LETTERS[1:5],
  VALUE = rnorm(5)
)

D %>% 
  highlight_key(~NAME) %>%
  plot_ly(x=~VALUE, y = ~NAME, type ="bar", orientation = "h") %>% 
  highlight(on = "plotly_click",off = "plotly_doubleclick") %>%
  layout(barmode = "overlay")

enter image description here

Alternatively it is possible to use selectedpoints in plot_ly function.

like image 91
Patrik_P Avatar answered Jan 30 '26 10:01

Patrik_P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!