plotly / plotly/plotly.R

ggplotly with "tooltip = text" is not working in a bar graph with + and - data

Open
#1,929 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

ggplotly with "tooltip = text" does not work in a bar graph, where there is positive and negative data.

It does work if a very small number is added x_limits[1] inside geom_col.

It also works if "tooltip = text" is removed

This does not work:

library(tidyverse)

plot_data <- ggplot2::diamonds %>%
  mutate(cut = stringr::str_to_sentence(cut)) %>%
  group_by(cut) %>%
  summarise(average_price = mean(price)) %>%
  mutate(average_price = round(average_price / 1000, 1)) %>% 
  mutate(average_price = average_price - 4) %>% 
  ungroup() %>% 
  add_row(cut = "OK", average_price = NA) %>% 
  mutate(tip_text = "Hello!")

na_data <- plot_data %>% 
  filter(is.na(average_price)) 

breaks <- pretty(plot_data$average_price)

limits <- c(min(breaks), max(breaks))

plot_data <- plot_data %>% 
  mutate(id = row_number())

plot <- ggplot(data = plot_data) +
  geom_col(aes(y = average_price, x = cut, text = tip_text))  +
  coord_flip() +
  scale_y_continuous(breaks = breaks, limits = limits, expand = c(0, 0)) +
  geom_col(aes(y = limits[2], x = cut, text = tip_text), data = na_data) +
  geom_col(aes(y = limits[1], x = cut, text = tip_text), data = na_data)

plot  

plotly::ggplotly(plot, tooltip = "text") 

This does works:

library(tidyverse)

plot_data <- ggplot2::diamonds %>%
  mutate(cut = stringr::str_to_sentence(cut)) %>%
  group_by(cut) %>%
  summarise(average_price = mean(price)) %>%
  mutate(average_price = round(average_price / 1000, 1)) %>% 
  mutate(average_price = average_price - 4) %>% 
  ungroup() %>% 
  add_row(cut = "OK", average_price = NA) %>% 
  mutate(tip_text = "Hello!")

na_data <- plot_data %>% 
  filter(is.na(average_price)) 

breaks <- pretty(plot_data$average_price)

limits <- c(min(breaks), max(breaks))

plot_data <- plot_data %>% 
  mutate(id = row_number())

plot <- ggplot(data = plot_data) +
  geom_col(aes(y = average_price, x = cut, text = tip_text))  +
  coord_flip() +
  scale_y_continuous(breaks = breaks, limits = limits, expand = c(0, 0)) +
  geom_col(aes(y = limits[2], x = cut, text = tip_text), data = na_data) +
  geom_col(aes(y = limits[1] + 0.000001, x = cut, text = tip_text), data = na_data)

plot  

plotly::ggplotly(plot, tooltip = "text") 

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named. Start by running the provided R example with plotly::ggplotly() and compare tooltip behavior with and without the small y-limit offset. Done means tooltip = "text" works for the bar graph containing both positive and negative data without requiring that workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.