plotly / plotly/plotly.R

Unequal facet heights

Open
#2,100 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

In the reprex below, the first and last panels are noticeably taller than the others (note the y axis ticks are overlapping for the remaining panels).

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(plyr)
#> 
#> Attaching package: 'plyr'
#> The following objects are masked from 'package:plotly':
#> 
#>     arrange, mutate, rename, summarise

date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),
           sample(1:3, size=365, replace=T),
           sample(10:100, size=365, replace=T), 
           sample(1:50, size=365, replace=T),
           sample(1:20, size=365, replace=T),
           sample(50:100, size=365, replace=T),
           sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)

p <- ggplot(dat, aes(x=date, y=value, color=location, group=location)) +
    geom_line()+
  theme(panel.spacing.y = ggplot2::unit(1, "lines")) +
  facet_grid(product ~ ., scales = "fixed")

ggplotly(p)

(Also, the legend compresses the first few panels awkwardly, but that is a separate issue).

The problem also occurs with facet_wrap():

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(plyr)
#> 
#> Attaching package: 'plyr'
#> The following objects are masked from 'package:plotly':
#> 
#>     arrange, mutate, rename, summarise

date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),
           sample(1:3, size=365, replace=T),
           sample(10:100, size=365, replace=T), 
           sample(1:50, size=365, replace=T),
           sample(1:20, size=365, replace=T),
           sample(50:100, size=365, replace=T),
           sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)

p <- ggplot(dat, aes(x=date, y=value, color=location, group=location)) +
    geom_line()+
  theme(panel.spacing.y = ggplot2::unit(1, "lines")) +
  facet_wrap(ncol = 1, vars(product), scales = "fixed")

ggplotly(p)

This issue doesn't appear to be coming from ggplot, but rather from ggplotly:

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(plyr)
#> 
#> Attaching package: 'plyr'
#> The following objects are masked from 'package:plotly':
#> 
#>     arrange, mutate, rename, summarise

date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),
           sample(1:3, size=365, replace=T),
           sample(10:100, size=365, replace=T), 
           sample(1:50, size=365, replace=T),
           sample(1:20, size=365, replace=T),
           sample(50:100, size=365, replace=T),
           sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)

p <- ggplot(dat, aes(x=date, y=value, color=location, group=location)) +
    geom_line()+
  theme(panel.spacing.y = ggplot2::unit(1, "lines")) +
  facet_grid(product ~., scales = "fixed")

print(p)

Created on 2022-01-12 by the reprex package (v2.0.1)

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

Start by running the supplied R reprex with ggplotly(), then compare its facet_grid() and facet_wrap() output with the correctly sized ggplot result. Trace the ggplotly facet layout conversion and identify where panel heights or spacing become unequal. Done means both examples render equal-height panels without overlapping y-axis ticks.

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 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.