1
votes

I'm trying to set the marker size using a column within the data, however the size of markers with values of 1, 2, 5, etc. change based on the underlying data. Is it at all possible to set the sizes of markers with the exact pixel size? That way when my data changes markers of the same size will always have the same associated value regardless of the underlying data.

Here is a simple example:

This first one shows that all the markers are large (definitely not 1px as you might interpret from the variable n.

library(plotly)

dat <- data.frame(y=factor(c(1,1,2,2),levels=c(1,2),labels=c("GRP1","GRP2")),
                  x=c(1,2,3,4),
                  n=c(1,1,1,1),
                  grp=c("GRP1","GRP1","GRP2","GRP2"))

plot_ly(dat) %>%
  add_markers(x = ~x, 
              y = ~y, color = ~grp, 
              size = ~n, 
              opacity = .7) %>%
  layout(showlegend=F)

enter image description here

This second one has adjusted for the size, but only in one group. A size of 1 is slightly different between the two groups.

dat2 <- data.frame(y=factor(c(1,1,2,2),levels=c(1,2),labels=c("GRP1","GRP2")),
                   x=c(1,2,3,4), 
                   n=c(1,2,1,1),
                   grp=c("GRP1","GRP1","GRP2","GRP2"))

plot_ly(dat2) %>%
  add_markers(x = ~x, 
              y = ~y, color = ~grp, 
              size = ~n, 
              opacity = .7) %>%
  layout(showlegend=F)

enter image description here

I tried playing around with the options sizes and marker=list(sizeref=0.1,sizemode="area") but those only seem to fix part of the problem.

plot_ly(dat2) %>%
  add_markers(x = ~x, 
              y = ~y, color = ~grp, 
              size = ~n, 
              sizes = c(10,50),
              opacity = .7) %>%
  layout(showlegend=F)

plot_ly(dat2) %>%
  add_markers(x = ~x, 
              y = ~y, color = ~grp, 
              size = ~n, 
              marker=list(sizeref=0.1, sizemode="area"),
              opacity = .7) %>%
  layout(showlegend=F)

I'm looking for a simple way to provide the size of each marker by giving it the size in pixels explicitly. I also tried removing the ~ in size = ~n but that just gives me an error. Am I missing something... is this possible?

Thanks.

EDIT:

This example clearly shows the issue, note that I am multiplying the size by 400 and dat2 is defined as above:

plot_ly(dat2) %>%
  add_markers(x = ~x, 
              y = ~y,  color = ~grp, 
              size = ~I(400*n), 
              text = ~paste0("Size of marker: ",n),
              opacity = .7,
              type="scatter") %>%
  layout(showlegend=F)

enter image description here

The two blue points (which have a size of 400*1) are not the same size as the first green point (size of 400*1).

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] plotly_4.5.6  ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.10       knitr_1.15.1       magrittr_1.5       munsell_0.4.3      colorspace_1.3-2   viridisLite_0.2.0 
 [7] R6_2.2.0           stringr_1.2.0      httr_1.2.1         plyr_1.8.4         dplyr_0.5.0        tools_3.3.3       
[13] grid_3.3.3         gtable_0.2.0       DBI_0.6-1          htmltools_0.3.5    yaml_2.1.14        lazyeval_0.2.0    
[19] assertthat_0.2.0   digest_0.6.12      rprojroot_1.2      tibble_1.3.0       purrr_0.2.2        RColorBrewer_1.1-2
[25] tidyr_0.6.1        base64enc_0.1-3    htmlwidgets_0.8    evaluate_0.10      rmarkdown_1.4      stringi_1.1.5     
[31] backports_1.0.5    scales_0.4.1       jsonlite_1.4      
1
this is a plotly related question, I think you can remove shiny tag - HubertL
The problem exists because of the reactive shiny object... The underlying data can be changed and the new plot draws markers that are a different size. I want a size of 1 or 2 to be the exact same regardless of the data from the reactive object in shiny. - Mark Nielsen
if so please post a minimum reproducible example using shiny - HubertL

1 Answers

2
votes

The answer according to cpsievert on github is to use the lower-level controls of size within the marker argument:

plot_ly(x = 1:4, y = 1:4, size = 1:4, marker = list(size = 4:1 * 10, sizemode = "diameter"))

Using his example my code now becomes:

library(plotly)

dat <- data.frame(y=factor(c(1,1,2,2),levels=c(1,2),labels=c("GRP1","GRP2")),
                  x=c(1,2,3,4),
                  n=c(1,1,1,1),
                  grp=c("GRP1","GRP1","GRP2","GRP2"))

plot_ly(dat) %>%
  add_markers(x = ~x, 
              y = ~y, color = ~grp, 
              marker = list(size=~I(n*10)),
              opacity = .7) %>%
  layout(showlegend=F)

dat2 <- data.frame(y=factor(c(1,1,2,2),levels=c(1,2),labels=c("GRP1","GRP2")),
                   x=c(1,2,3,4),
                   n=c(1,2,1,1),
                   grp=c("GRP1","GRP1","GRP2","GRP2"))

plot_ly(dat2) %>%
  add_markers(x = ~x, 
              y = ~y,  color = ~grp, 
              marker = list(size=~I(n*10)),
              opacity = .7) %>%
  layout(showlegend=F)