2

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      
Frank Harrell
  • 1,954
  • 2
  • 18
  • 36
  • 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 Aug 16 '18 at 11:10
  • 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 Aug 16 '18 at 13:22
  • That's a link back to this same page - did you mean to give a link to a different page? – Frank Harrell Aug 16 '18 at 16:15
  • sorry, have you seen this: https://stackoverflow.com/questions/39948151/in-r-plotly-subplot-graph-how-to-show-only-one-legend – MLavoie Aug 16 '18 at 16:16
  • 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 Aug 16 '18 at 16:54
  • The legends are appearing correctly now but for each of the `add_markers` plots only first first of each triplet of points is drawn. I've expanded the example in the original post. I must be missing something basic about `plotly`. – Frank Harrell Aug 18 '18 at 15:16
  • When I run your new code, I have 3 points in each of the top two graphics. – MLavoie Aug 19 '18 at 10:50
  • I must be doing something odd. I run at the command line which gives an auto-preview in Google Chrome and I see only the one point in each part. Are you knitting to html? How are you rendering the graphic? – Frank Harrell Aug 19 '18 at 17:40
  • in R studio. Maybe add your `sessionInfo()` to your post. – MLavoie Aug 19 '18 at 17:54
  • Adding sessionInfo now. I ran this as an ordinary R script in RStudio and only saw one point per panel. I did not run this as a markdown document or using knit to html. I'm just looking at the Viewer in RStudio. – Frank Harrell Aug 19 '18 at 21:30
  • I am using `plotly_4.7.1.9000 ggplot2_2.2.1.9000`. – MLavoie Aug 19 '18 at 22:12
  • This remains mysterious, but I have developed a more comprehensive function using a data frame to drive a set of `plotly` graphics combined with `subplot`, and everything is working as it should as long as I don't use `add_trace` but instead use `add_markers` and `add_lines`. I do sort the data frame in this new example, which perhaps has something to do with it? – Frank Harrell Aug 20 '18 at 12:32

2 Answers2

2

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

Ameya
  • 1,712
  • 1
  • 14
  • 29
1

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

MLavoie
  • 9,671
  • 41
  • 36
  • 56