Skip to content

Incorrect z-ordering with geom_errorbarh #513

Closed

Description

@humburg

I've encountered a problem where layers appear in the wrong order when using geom_errorbarh to plot error bars. Consider the following example:

library(plotly)

df <- data.frame(x=1:5, y=1:5, left=0:4, right=2:6, label=letters[1:5], 
                 group=factor(c('one', 'one', 'two', 'two', 'one')))
fig1 <- ggplot(df, aes(x=x, y=y, text=label, colour=group)) + 
  geom_rect(fill='lightgrey', colour='lightgrey', xmin=3, xmax=4, ymin=-4, ymax=7) +
  geom_errorbarh(aes(xmin=left, xmax=right)) + geom_point() +
  theme_bw()

fig1  ## works as expected
ggplotly(fig1)  ## The grey rectangle is plotted above the error bars instead of below them

A workaround is to use geom_segment instead

fig2 <- ggplot(df, aes(x=x, y=y, text=label, colour=group)) + 
  geom_rect(fill='lightgrey', colour='lightgrey', xmin=3, xmax=4, ymin=-4, ymax=7) +
  geom_segment(aes(x=left, xend=right, yend=y)) + geom_point() +
  theme_bw()

ggplotly(fig2)  ## works

Here is the output from devtools::session_info()

Session info ------------------------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.2.3 (2015-12-10)
 system   x86_64, mingw32             
 ui       RStudio (0.99.489)          
 language (EN)                        
 collate  English_Australia.1252      
 tz       Australia/Sydney            
 date     2016-03-16                  

Packages ----------------------------------------------------------------------------------------------------------------------------------------------------------------
 package     * version     date       source                          
 assertthat    0.1         2013-12-06 CRAN (R 3.2.0)                  
 base64enc     0.1-3       2015-07-28 CRAN (R 3.2.3)                  
 colorspace    1.2-6       2015-03-11 CRAN (R 3.2.1)                  
 curl          0.9.6       2016-02-17 CRAN (R 3.2.3)                  
 DBI           0.3.1       2014-09-24 CRAN (R 3.2.0)                  
 devtools      1.10.0.9000 2016-02-22 Github (hadley/devtools@24728f7)
 digest        0.6.9       2016-01-08 CRAN (R 3.2.3)                  
 dplyr         0.4.3       2015-09-01 CRAN (R 3.2.3)                  
 ggplot2     * 2.1.0       2016-03-01 CRAN (R 3.2.3)                  
 git2r         0.13.1      2015-12-10 CRAN (R 3.2.3)                  
 gridExtra     2.2.1       2016-02-29 CRAN (R 3.2.3)                  
 gtable        0.2.0       2016-02-26 CRAN (R 3.2.3)                  
 htmltools     0.3         2015-12-29 CRAN (R 3.2.3)                  
 htmlwidgets   0.6         2016-02-25 CRAN (R 3.2.3)                  
 httr          1.1.0       2016-01-28 CRAN (R 3.2.3)                  
 jsonlite      0.9.19      2015-11-28 CRAN (R 3.2.3)                  
 knitr         1.12.3      2016-01-22 CRAN (R 3.2.3)                  
 labeling      0.3         2014-08-23 CRAN (R 3.2.0)                  
 magrittr      1.5         2014-11-22 CRAN (R 3.2.0)                  
 memoise       1.0.0       2016-01-29 CRAN (R 3.2.3)                  
 munsell       0.4.3       2016-02-13 CRAN (R 3.2.3)                  
 plotly      * 3.4.3       2016-03-16 Github (ropensci/plotly@908cbf1)
 plyr          1.8.3       2015-06-12 CRAN (R 3.2.0)                  
 R6            2.1.2       2016-01-26 CRAN (R 3.2.3)                  
 Rcpp          0.12.3      2016-01-10 CRAN (R 3.2.3)                  
 scales        0.4.0       2016-02-26 CRAN (R 3.2.3)                  
 tidyr         0.4.1       2016-02-05 CRAN (R 3.2.3)                  
 viridis       0.3.4       2016-03-12 CRAN (R 3.2.4)                  
 withr         1.0.1       2016-02-04 CRAN (R 3.2.3)                  
 yaml          2.1.13      2014-06-12 CRAN (R 3.2.0)  

Activity

  1. cpsievert commented on Mar 25, 2016

    @cpsievert
    Collaborator

    Fixed via 03d3e54

  2. humburg commented on Apr 1, 2016

    @humburg
    Author

    Unfortunately this still doesn't work for me with the current version (3.4.9). I'm now not getting any error bars (using the above example):

    ggplotly(fig1)

    image

    Updated devtools::session_info()

    Session info ------------------------------------------------------------------------------------------------------------------------------------------------------------
     setting  value                       
     version  R version 3.2.3 (2015-12-10)
     system   x86_64, mingw32             
     ui       RStudio (0.99.489)          
     language (EN)                        
     collate  English_Australia.1252      
     tz       Australia/Sydney            
     date     2016-04-01                  
    
    Packages ----------------------------------------------------------------------------------------------------------------------------------------------------------------
     package     * version     date       source                          
     assertthat    0.1         2013-12-06 CRAN (R 3.2.0)                  
     base64enc     0.1-3       2015-07-28 CRAN (R 3.2.3)                  
     colorspace    1.2-6       2015-03-11 CRAN (R 3.2.1)                  
     DBI           0.3.1       2014-09-24 CRAN (R 3.2.0)                  
     devtools      1.10.0.9000 2016-02-22 Github (hadley/devtools@24728f7)
     digest        0.6.9       2016-01-08 CRAN (R 3.2.3)                  
     dplyr         0.4.3       2015-09-01 CRAN (R 3.2.3)                  
     ggplot2     * 2.1.0       2016-03-01 CRAN (R 3.2.3)                  
     gridExtra     2.2.1       2016-02-29 CRAN (R 3.2.3)                  
     gtable        0.2.0       2016-02-26 CRAN (R 3.2.3)                  
     htmltools     0.3.5       2016-03-21 CRAN (R 3.2.4)                  
     htmlwidgets   0.6         2016-02-25 CRAN (R 3.2.3)                  
     httr          1.1.0       2016-01-28 CRAN (R 3.2.3)                  
     jsonlite      0.9.19      2015-11-28 CRAN (R 3.2.3)                  
     labeling      0.3         2014-08-23 CRAN (R 3.2.0)                  
     magrittr      1.5         2014-11-22 CRAN (R 3.2.0)                  
     memoise       1.0.0       2016-01-29 CRAN (R 3.2.3)                  
     munsell       0.4.3       2016-02-13 CRAN (R 3.2.3)                  
     plotly      * 3.4.9       2016-03-29 Github (ropensci/plotly@03d3e54)
     plyr          1.8.3       2015-06-12 CRAN (R 3.2.0)                  
     R6            2.1.2       2016-01-26 CRAN (R 3.2.3)                  
     Rcpp          0.12.4      2016-03-26 CRAN (R 3.2.4)                  
     scales        0.4.0       2016-02-26 CRAN (R 3.2.3)                  
     tidyr         0.4.1       2016-02-05 CRAN (R 3.2.3)                  
     viridis       0.3.4       2016-03-12 CRAN (R 3.2.4)                  
     yaml          2.1.13      2014-06-12 CRAN (R 3.2.0)
    
  3. added a commit that references this issue on Apr 1, 2016
    cf58771
  4. cpsievert commented on Apr 1, 2016

    @cpsievert
    Collaborator

    Thanks for reporting, this will be fixed soon.

    PS. unfortunately, given plotly.js' current infrastructure for error bars, I don't think it is possible to faithfully convert ggplot2's GeomErrorbar and guarantee a sensible legend. But, you can layer points before the bars to get a sensible legend.

    ggplot(df, aes(x=x, y=y, text=label, colour=group)) + 
      geom_rect(fill='lightgrey', colour='lightgrey', xmin=3, xmax=4, ymin=-4, ymax=7) +
      geom_point() + geom_errorbarh(aes(xmin=left, xmax=right)) + 
      theme_bw()
  5. humburg commented on Apr 1, 2016

    @humburg
    Author

    Thanks for taking care of this so promptly.

  6. added a commit that references this issue on Apr 1, 2016
    1d56b19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions