Search code examples
plotlyr-plotly

Avoiding duplicate legends with R plotly subplot results in missing points


Updated with more complete example

Related to http://stackoverflow.com/questions/26939121, I am producing a series of marker-type plots with R plotly 4.8 that are combined with plotly::subplot, and I'm hiding the legend in the first of each pair of the component plots so that the final plot does not have duplicated legends. But when doing this, only the first (x,y) point is shown for each of the two data frames being plotted (the top two plots). The test code that demonstrates this is below.

require(plotly)
set.seed(1)

a <- data.frame(x=1:3, y=1:3)
b <- data.frame(x=(1:3)+.1, y=(1:3)+.1)
xu <- runif(1000, 0, 3)
xn <- (rnorm(1000) + 3) / 2
co <- 'black'
p <- plot_ly()
pa <- add_markers(p, mode='marker',
                  data=a, x=~x, y=~y, name='j', legendgroup='j',
                  size=I(5), color=I(co),
                  showlegend=FALSE)

pb <- add_markers(p, mode='marker',
                  data=b, x=~x, y=~y, name='j', legendgroup='j',
                  size=I(5), color=I(co),
                  showlegend=TRUE)

pc <- add_histogram(p, x=~xu, name='k', color=I('black'),
                    legendgroup='k', showlegend=FALSE)
pd <- add_histogram(p, x=~xn, name='k', color=I('black'),
                    legendgroup='k', showlegend=TRUE)

plotly::subplot(pa, pb, pc, pd, shareX=TRUE, shareY=FALSE, titleX=TRUE, nrows=4)

Thanks for any pointers. To have points suppressed from the output of add_markers I must have some basic misunderstanding of plotly.

Here's the output of sessionInfo():

R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2.2 plotly_4.8.0   ggplot2_3.0.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18       RColorBrewer_1.1-2 pillar_1.3.0       compiler_3.5.1     later_0.7.3        plyr_1.8.4        
 [7] bindr_0.1.1        tools_3.5.1        digest_0.6.15      jsonlite_1.5       tibble_1.4.2       gtable_0.2.0      
[13] viridisLite_0.3.0  pkgconfig_2.0.2    rlang_0.2.2        shiny_1.1.0        rstudioapi_0.7     crosstalk_1.0.0   
[19] yaml_2.2.0         withr_2.1.2        dplyr_0.7.6        httr_1.3.1         htmlwidgets_1.2    grid_3.5.1        
[25] tidyselect_0.2.4   glue_1.3.0         data.table_1.11.4  R6_2.2.2           purrr_0.2.5        tidyr_0.8.1       
[31] magrittr_1.5       scales_1.0.0       promises_1.0.1     htmltools_0.3.6    assertthat_0.2.0   xtable_1.8-2      
[37] mime_0.5           colorspace_1.3-2   httpuv_1.4.5       lazyeval_0.2.1     munsell_0.5.0      crayon_1.3.4      

Solution

  • Does this help? I think the issue was in the way you had specified size. The attribute marker controls the size of the points.

    require(plotly)
    set.seed(1)
    
    a <- data.frame(x=1:3, y=1:3)
    b <- data.frame(x=(1:3)+.1, y=(1:3)+.1)
    xu <- runif(1000, 0, 3)
    xn <- (rnorm(1000) + 3) / 2
    co <- 'black'
    p <- plot_ly()
    # attribute 'marker' controls size of points
    pa <- add_markers(p, 
                      data=a, x=~x, y=~y, name='j', legendgroup='j',
                      marker = list(size = 5), color=I(co),
                      showlegend=FALSE)
    
    pb <- add_markers(p, 
                      data=b, x=~x, y=~y, name='j', legendgroup='j',
                      marker = list(size = 5), color=I(co),
                      showlegend=TRUE)
    
    pc <- add_histogram(p, x=~xu, name='k', color=I('black'),
                        legendgroup='k', showlegend=FALSE)
    pd <- add_histogram(p, x=~xn, name='k', color=I('black'),
                        legendgroup='k', showlegend=TRUE)
    
    plotly::subplot(pa, pb, pc, pd, shareX=TRUE, shareY=FALSE, titleX=TRUE, nrows=4)
    

    Note that mode = 'markers' (note the plural) is not required if you're using add_markers. It is, however, required if you use the more general add_trace.

    enter image description here