rstudio / ggvis

Interactive grammar of graphics for R
Other
713 stars 171 forks source link

layer_ribbons does not work as expected #476

Open slowkow opened 6 years ago

slowkow commented 6 years ago

I'm trying to show 3 lines, and each line should have a ribbon that shows the confidence interval.

library(magrittr)
library(ggvis)
d <- read.table(textConnection(
'  time group median  low high
1     0     A   1.29 1.09 1.43
2     0     B   1.29 1.09 1.43
3     0     C   1.29 1.09 1.43
4     2     A   5.18 5.04 5.75
5     2     B   7.76 7.75 8.04
6     2     C   8.76 8.67 8.94
7     4     A   4.10 3.93 4.31
8     4     B   7.67 7.18 7.86
9     4     C   8.57 8.17 8.76
10    6     A   3.67 3.62 3.83
11    6     B   7.18 6.92 7.59
12    6     C   8.94 8.72 9.33'
))
d$time <- factor(d$time)
d$group <- factor(d$group)

# NOT OK
d %>%
  ggvis(
    x = ~time,
    stroke = ~group,
    fill = ~group
  ) %>%
  layer_ribbons(y = ~low, y2 = ~high) %>%
  layer_lines(y = ~median)

image

The plot works OK without the ribbons:

# OK
d %>%
  ggvis(
    x = ~time,
    stroke = ~group
  ) %>%
  layer_lines(y = ~median)

image

Click here for my Session Info
> devtools::session_info()
Session info -------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.1.414)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2018-01-31                  

Packages -----------------------------------------------------------------------------------------------------------------------------------------
 package      * version    date       source                            
 assertthat     0.2.0      2017-04-11 CRAN (R 3.4.0)                    
 backports      1.1.1      2017-09-25 CRAN (R 3.4.2)                    
 base         * 3.4.3      2017-12-07 local                             
 base64         2.0        2016-05-10 cran (@2.0)                       
 bindr          0.1        2016-11-13 CRAN (R 3.4.0)                    
 bindrcpp     * 0.2        2017-06-17 CRAN (R 3.4.0)                    
 callr          1.0.0      2016-06-18 CRAN (R 3.4.0)                    
 cli            1.0.0      2017-11-05 CRAN (R 3.4.2)                    
 clipr          0.4.0      2017-11-03 CRAN (R 3.4.2)                    
 colorspace     1.3-2      2016-12-14 CRAN (R 3.4.0)                    
 compiler       3.4.3      2017-12-07 local                             
 crayon         1.3.4      2017-09-16 CRAN (R 3.4.1)                    
 datasets     * 3.4.3      2017-12-07 local                             
 devtools       1.13.4     2017-11-09 CRAN (R 3.4.2)                    
 digest         0.6.13     2017-12-14 cran (@0.6.13)                    
 dplyr        * 0.7.4      2017-09-28 CRAN (R 3.4.2)                    
 DT           * 0.2        2016-08-09 CRAN (R 3.4.0)                    
 evaluate       0.10.1     2017-06-24 CRAN (R 3.4.1)                    
 formatR        1.5        2017-04-25 CRAN (R 3.4.0)                    
 ggplot2      * 2.2.1.9000 2017-12-22 Github (tidyverse/ggplot2@a637c67)
 ggvis        * 0.4.3      2016-07-22 CRAN (R 3.4.0)                    
 glue           1.2.0      2017-10-29 CRAN (R 3.4.2)                    
 graphics     * 3.4.3      2017-12-07 local                             
 grDevices    * 3.4.3      2017-12-07 local                             
 grid           3.4.3      2017-12-07 local                             
 gridExtra    * 2.3        2017-09-09 CRAN (R 3.4.1)                    
 gtable         0.2.0      2016-02-26 CRAN (R 3.4.0)                    
 htmltools      0.3.6      2017-04-28 CRAN (R 3.4.0)                    
 htmlwidgets    1.0        2018-01-20 cran (@1.0)                       
 httpuv         1.3.5      2017-07-04 cran (@1.3.5)                     
 jsonlite       1.5        2017-06-01 cran (@1.5)                       
 knitr          1.17       2017-08-10 CRAN (R 3.4.1)                    
 labeling       0.3        2014-08-23 CRAN (R 3.4.0)                    
 lazyeval       0.2.1      2017-10-29 CRAN (R 3.4.2)                    
 limma        * 3.34.2     2017-11-27 Bioconductor                      
 magrittr     * 1.5        2014-11-22 CRAN (R 3.4.0)                    
 MASS           7.3-47     2017-02-26 CRAN (R 3.4.3)                    
 memoise        1.1.0      2017-04-21 CRAN (R 3.4.0)                    
 methods      * 3.4.3      2017-12-07 local                             
 mime           0.5        2016-07-07 CRAN (R 3.4.0)                    
 miniUI         0.1.1      2016-01-15 CRAN (R 3.4.0)                    
 munsell        0.4.3      2016-02-13 CRAN (R 3.4.0)                    
 openssl        0.9.9      2017-11-10 CRAN (R 3.4.2)                    
 pillar         1.1.0      2018-01-14 cran (@1.1.0)                     
 pkgconfig      2.0.1      2017-03-21 CRAN (R 3.4.0)                    
 plyr           1.8.4      2016-06-08 CRAN (R 3.4.0)                    
 R6             2.2.2      2017-06-17 cran (@2.2.2)                     
 RColorBrewer * 1.1-2      2014-12-07 CRAN (R 3.4.0)                    
 Rcpp           0.12.15    2018-01-20 cran (@0.12.15)                   
 reprex         0.1.1      2017-01-13 CRAN (R 3.4.0)                    
 reshape2       1.4.3      2017-12-11 cran (@1.4.3)                     
 rlang          0.1.6      2017-12-21 cran (@0.1.6)                     
 rmarkdown      1.8        2017-11-17 CRAN (R 3.4.2)                    
 rprojroot      1.2        2017-01-16 CRAN (R 3.4.0)                    
 rstudioapi     0.7        2017-09-07 CRAN (R 3.4.1)                    
 scales       * 0.5.0.9000 2017-12-01 Github (hadley/scales@d767915)    
 shiny        * 1.0.5      2017-08-23 CRAN (R 3.4.1)                    
 shinyjs      * 0.9.1      2017-06-29 CRAN (R 3.4.1)                    
 stats        * 3.4.3      2017-12-07 local                             
 stringi        1.1.6      2017-11-17 CRAN (R 3.4.2)                    
 stringr      * 1.2.0      2017-02-18 CRAN (R 3.4.0)                    
 tibble         1.4.2      2018-01-22 cran (@1.4.2)                     
 tools          3.4.3      2017-12-07 local                             
 utf8           1.1.3      2018-01-03 cran (@1.1.3)                     
 utils        * 3.4.3      2017-12-07 local                             
 vegalite     * 0.7.1      2018-01-31 Github (hrbrmstr/vegalite@8a3dc26)
 viridis      * 0.4.0      2017-03-27 CRAN (R 3.4.0)                    
 viridisLite  * 0.2.0      2017-03-24 CRAN (R 3.4.0)                    
 webshot        0.5.0      2017-11-29 cran (@0.5.0)                     
 whisker        0.3-2      2013-04-28 CRAN (R 3.4.0)                    
 withr          2.1.1.9000 2017-12-20 Github (jimhester/withr@df18523)  
 xtable         1.8-2      2016-02-05 CRAN (R 3.4.0)                    
 yaml           2.1.16     2017-12-12 cran (@2.1.16)      
slowkow commented 6 years ago

I found a workaround for this particular case:

ggvis() %>%
  layer_ribbons(
    data = subset(d, group == "A"),
    x = ~time, y = ~low, y2 = ~high, fill := "pink"
  ) %>%
  layer_paths(
    data = subset(d, group == "A"),
    x = ~time, y = ~median, stroke := "red"
  ) %>%
  layer_ribbons(
    data = subset(d, group == "B"),
    x = ~time, y = ~low, y2 = ~high, fill := "skyblue"
  ) %>%
  layer_paths(
    data = subset(d, group == "B"),
    x = ~time, y = ~median, stroke := "blue"
  ) %>%
  layer_ribbons(
    data = subset(d, group == "C"),
    x = ~time, y = ~low, y2 = ~high, fill := "lightgreen"
  ) %>%
  layer_paths(
    data = subset(d, group == "C"),
    x = ~time, y = ~median, stroke := "green"
  )

image