1
votes

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      
2
in pa, if I replace color=I('black') with color=~y it works. In order to get a legend you need to attribute a color or a size. - MLavoie
I'm not clear on that. I don't want a separate color for each value of y, and I'mm going to be adding lines and other traces and I want to be able to click some of the traces off by clicking on their part of the legend. - Frank Harrell
That's a link back to this same page - did you mean to give a link to a different page? - Frank Harrell
sorry, have you seen this: stackoverflow.com/questions/39948151/… - MLavoie
Thanks. The main point I got from that was to switch the order of showlegend=TRUE and FALSE. I tried that but still no legend and only one point shown per plot. I tried removing size=I(5), color=I('black') and all the points showed, but the two plots used different color points and the legend still did not appear. - Frank Harrell

2 Answers

2
votes

Does this help? I think the issue was in the way you had specified size.

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, 
                  data=a, x=~x, y=~y, name='j', legendgroup='j',
                  marker = list(size = 5), color=I(co), # attribute 'marker' controls size of points
                  showlegend=FALSE)

pb <- add_markers(p, 
                  data=b, x=~x, y=~y, name='j', legendgroup='j',
                  marker = list(size = 5), color=I(co),# attribute 'marker' controls size of points

                  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

0
votes

Maybe someone will have an answer with plot_ly(), but here is an alternative with ggplot2() and ggplotly().

You could try this:

c <- data.frame(x=c(1, 2, 3, 1, 2, 3), y=c(1, 2, 3, 1, 2, 3), c = c("PA", "PA", "PA", "PB", "PB", "PB"), z = c(1, 1, 1, 1, 1, 1))

ggplotly(ggplot(data = c, aes(x = x, y =y)) + 
           geom_point(aes(color = as.factor(z))) + 
           facet_wrap(~ c, ncol = 1) + theme_bw() +  
           theme(
             strip.background = element_blank(),
             strip.text.x = element_blank()
           ) + 
           scale_color_manual(name = "", values = c("black")))

enter image description here