plotly / plotly/plotly.R

Unexpected behavior with geom_rect and ids arg in ggplotly()

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

Nobody has claimed this yet.

difficulty: high ggplotly priority: low
Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

Expected behavior demonstrated by geom_point. See ggplotly(gg1) below.

Unexpected behavior when using geom_rect with the ids arg across frames with changing number of observations. See ggplotly(gg2) and ggplotly(gg3) below.

### Win 10 x64, Rstudio
library(ggplot2) # ggplot2_3.0.0 
library(plotly)  # plotly_4.7.1
data(gapminder, package = "gapminder")

# filtering out some countries (soframes have differing number of obs)
sub_countries <- unique(gapminder$country)[1:10]
gapminder2 <- gapminder %>% 
  filter(((year != 1952) | (country %in% sub_countries)) & year < 1970)

# using geom_point (works)
gg1 <- ggplot(gapminder2) +
  geom_point(aes(x = gdpPercap, y = lifeExp, 
                 ids = country,
                 frame = year)) +
  scale_x_log10()
ggplotly(gg1) # correct: sub, full, full, full

# using geom_rect (doesn't work with ids arg)
gg2 <- ggplot(gapminder2) +
  geom_rect(aes(xmin = gdpPercap, ymin = lifeExp, 
                xmax = 1.05 * gdpPercap, ymax = 1.05 * lifeExp, 
                ids = country,
                frame = year))  +
  scale_x_log10()
ggplotly(gg2) # incorrect: sub, sub, sub, sub

# using geom_rect, subset on 2nd frame (doesn't work with ids arg)
gapminder3 <- gapminder %>% 
  filter(((year != 1957) | (country %in% sub_countries)) & year < 1970)

gg3 <- ggplot(gapminder3) +
  geom_rect(aes(xmin = gdpPercap, ymin = lifeExp, 
                xmax = 1.05 * gdpPercap, ymax = 1.05 * lifeExp, 
                ids = country,
                frame = year))  +
  scale_x_log10()
ggplotly(gg3) # incorrect: full, sub moves, full, full
  # frame year 1952 -> 1957 != 1962 -> 1957 != 1967 -> 1957

Cheers,
Nick

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

Run the supplied R reproduction with ggplot2 and plotly, comparing geom_point and geom_rect across frames with changing observation counts. Trace the ggplotly conversion path for geom_rect, ids, and frame handling. Done means geom_rect preserves the correct observations and ids in every frame, matching the demonstrated expected behavior.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.