plotly / plotly/plotly.R

ggplotly with points, error bars and facets error

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

I am building a ggplot which has points, error bars and facets. I use the ggplotly() function to create a plotly graph, but I get the error returned below. With a bit of tracing back, I found the error to be in ggplotly() -> layers2traces() -> to_basic() when it's applied to the error bar geometry (it works when applied to the points geometry). In particular, it crashes on this line

data$width <- (data[["xmax"]] - data[["x"]])/(data[["x_max"]] - 
                                                data[["x_min"]])

Because x is not an element in data. In a slightly larger case than the reprex below, but with the same properties, I found the following when applying head() to the data object in the browser:

Browse[4]> head(data)
PANEL  colour   x.x         y      ymin      ymax group x_plotlyDomain
1     1 #00BFC4 1.125 0.3621916 0.3348249 0.3891862     2           2004
2     1 #F8766D 0.875 0.3628851 0.3388994 0.3877805     1           2004
3     1 #00BFC4 2.125 0.3107964 0.2823060 0.3398029     4           2006
4     1 #F8766D 1.875 0.3113829 0.2856144 0.3391679     3           2006
5     1 #00BFC4 3.125 0.3019772 0.2791706 0.3261124     6           2008
6     1 #F8766D 2.875 0.3030195 0.2814583 0.3247936     5           2008
y_plotlyDomain  xmin  xmax colour_plotlyDomain size linetype width alpha
1      0.3621916 1.125 1.125             Model 2  1.5        1     0    NA
2      0.3628851 0.875 0.875             Model 1  1.5        1     0    NA
3      0.3107964 2.125 2.125             Model 2  1.5        1     0    NA
4      0.3113829 1.875 1.875             Model 1  1.5        1     0    NA
5      0.3019772 3.125 3.125             Model 2  1.5        1     0    NA
6      0.3030195 2.875 2.875             Model 1  1.5        1     0    NA
hovertext
1 year: 2004<br />fit: 0.3621916<br />model: Model 2<br />lower: 0.3348249<br />upper: 0.3891862
2 year: 2004<br />fit: 0.3628851<br />model: Model 1<br />lower: 0.3388994<br />upper: 0.3877805
3 year: 2006<br />fit: 0.3107964<br />model: Model 2<br />lower: 0.2823060<br />upper: 0.3398029
4 year: 2006<br />fit: 0.3113829<br />model: Model 1<br />lower: 0.2856144<br />upper: 0.3391679
5 year: 2008<br />fit: 0.3019772<br />model: Model 2<br />lower: 0.2791706<br />upper: 0.3261124
6 year: 2008<br />fit: 0.3030195<br />model: Model 1<br />lower: 0.2814583<br />upper: 0.3247936
ROW COL  x.y SCALE_X SCALE_Y xaxis yaxis xanchor yanchor x_min x_max     y_min
1   1   1 Male       1       1 xaxis yaxis       y       x   0.4   5.6 0.1775765
2   1   1 Male       1       1 xaxis yaxis       y       x   0.4   5.6 0.1775765
3   1   1 Male       1       1 xaxis yaxis       y       x   0.4   5.6 0.1775765
4   1   1 Male       1       1 xaxis yaxis       y       x   0.4   5.6 0.1775765
5   1   1 Male       1       1 xaxis yaxis       y       x   0.4   5.6 0.1775765
6   1   1 Male       1       1 xaxis yaxis       y       x   0.4   5.6 0.1775765
y_max
1 0.5294233
2 0.5294233
3 0.5294233
4 0.5294233
5 0.5294233
6 0.5294233

The full reprex is below.

library(tibble)
library(ggplot2)
library(plotly)
tmp <- tibble(
  year = rep(2004,4), 
  x = factor(c(1,2,1,2), labels=c("Male", "Female")), 
  fit = c(0.3628851, 0.3698281, 0.3621916, 0.3689059), 
  lower = c(0.3388994,0.3408131,0.3348249,0.3419387), 
  upper = c(0.3877805, 0.3998003, 0.3891862, 0.3980086), 
  model = factor(c(1,1,2,2), labels=c("Model 1", "Model 2"))
)

g <- ggplot(tmp, aes(x=year, y=fit, colour=model, ymin=lower, ymax=upper)) + 
  geom_point(position=position_dodge(width=.5), size=3) + 
  geom_errorbar(width=0, position=position_dodge(width=.5), size=1.5) + 
  facet_wrap(.~x)

ggplotly(g)
#> Error in `$<-.data.frame`(`*tmp*`, "width", value = numeric(0)): replacement has 0 rows, data has 4

Created on 2020-02-25 by the reprex package (v0.3.0)

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 reprex with ggplot2 and plotly, then trace ggplotly() through layers2traces() to to_basic() while processing the geom_errorbar layer. Compare the error-bar data columns with the fields accessed there; done means the supplied faceted plot converts successfully without the replacement-size error and related tests cover the case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.