Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly - Shape positions using subplot

Tags:

r

shiny

plotly

I am trying to plot multiple subplots with shapes on each plot. From the following code below, if I display one plot I get the following plot: Correct display of one plot

When I position 4 copies of this plot in a subplot I get the following: Incorrect display of 4 subplots. All shapes are drawn in same location

As you can see all of the 4 lines that should be drawn one on each subplot have all been drawn in the original position in the top left subplot. Any ideas how I can fix this?

** EDIT - using "paper"

If I use the "paper" for xref and yref in the declaration of my edge shapes the shapes do not scale properly. Maybe I am using "paper" in the wrong context here? I have attached an image below result of using xyref="paper"

library(plotly)
library(igraph)

tree <- make_tree(127,2)
tree_layout <- layout_as_tree(tree)
tree_layout_df <- as.data.frame(tree_layout)
Xn <- tree_layout_df[,1]
Yn <- tree_layout_df[,2]

edge_shapes  = list(
    type="line",
    line=list(color="#030303",width=5),
    opacity=0.3,
    x0 = 0, x1=0,
    x1 = 5,y1 = 5
)

reg_tree_plot <- plot_ly() %>%
    add_trace(x=~Xn[0:63], y=~Yn[0:63], type='scatter', width=700, height=500)

tree_layout <- layout(reg_tree_plot, shapes=edge_shapes)
x <- tree_layout
y <- tree_layout
z <- tree_layout

sub <- subplot(tree_layout,y,z,x,
              nrows=2,
              margin=0.05,
              shareX=FALSE,
              shareY=FALSE)
like image 215
Daniel Cole Avatar asked May 19 '17 19:05

Daniel Cole


People also ask

How do you use subplots in Plotly?

Simple SubplotFigures with subplots are created using the make_subplots function from the plotly. subplots module. Here is an example of creating a figure that includes two scatter traces which are side-by-side since there are 2 columns and 1 row in the subplot layout.

What does Add_trace do in Plotly?

Adding Traces New traces can be added to a plot_ly figure using the add_trace() method. This method accepts a plot_ly figure trace and adds it to the figure.

What is XREF in Plotly?

By default, text annotations have xref and yref set to "x" and "y" , respectively, meaning that their x/y coordinates are with respect to the axes of the plot.


2 Answers

I think this is pretty clearly a bug in subplot in plotly, which can be compensated for, but it is ugly and takes maintenance. You can see this if you publish it to the plotly site and look at the data in the layout. Note the appropriate coordinate systems seem to be:

  • "x1","y1" - 1st plot - upper left
  • "x2","y2" - 2nd plot - upper right
  • "x3","y3" - 3rd plot - lower left
  • "x4","y4" - 4th plot - left as an exercise for the reader

There are several issues here:

  • The shapes are being added to the layout without the proper coordinate references.
  • The y-coordinate systems for the 2nd and 4th plots are somehow different than the ones in the 1st and 3rd plots, and possibly related to the next issue.
  • The xaxis for the 1st and the 3rd plots are not being drawn, probably coordinate systems are also not being set correctly, but those structures seemed confused so I didn't look at it closely.
  • I think there was a typo in your code, you specified x0 twice in your creation of edge_shape.

Anyway, here is some code that does what you want (but with the axis bug).

library(plotly)
library(igraph)

tree <- make_tree(127,2)
tree_layout <- layout_as_tree(tree)
tree_layout_df <- as.data.frame(tree_layout)
Xn <- tree_layout_df[,1]
Yn <- tree_layout_df[,2]

get_edgeshape <- function(xref,yref,xfak=1,yfak=1){
  edge_shapes  = list(
    type="rect",
    line=list(color="#030303",width=5),
    opacity=0.3,
    x0 = 0*xfak, x1 = 5*xfak,
    y0 = 0*yfak, y1 = 5*yfak,
    xref=xref,
    yref=yref
  )
}
reg_tree_plot <- plot_ly() %>%
  add_trace(x=~Xn[0:63], y=~Yn[0:63], type='scatter', width=700, height=500)

ycor1 <- 2
ycor2 <- 2*1.125
tree_layout <- layout(reg_tree_plot, shapes=get_edgeshape("x1","y1",yfak=ycor1))
x <-           layout(reg_tree_plot, shapes=get_edgeshape("x2","y2",yfak=ycor2))
y <-           layout(reg_tree_plot, shapes=get_edgeshape("x3","y3",yfak=ycor1))
z <-           layout(reg_tree_plot, shapes=get_edgeshape("x4","y4",yfak=ycor2))

sub <- subplot(tree_layout,y,z,x,
               nrows=2,
               margin=0.05,
               shareX=F,
               shareY=F)
sub

And here is the result:

enter image description here

Note that it is not a great solution, if you change anything in the layout you will need to tweak the parameters. Only a plotly fix will truly address the pain. So I filed it as an issue on the github site.

I am using the latest plotly from github (4.7.0), installed with devtools::install_github("ropensci/plotly") and here is my sessionInfo():

R version 3.4.0 (2017-04-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

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 utils     datasets  methods   base     

other attached packages:
[1] igraph_1.0.1  plotly_4.7.0  ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.10      magrittr_1.5      devtools_1.12.0   munsell_0.4.3     xtable_1.8-2      viridisLite_0.2.0 colorspace_1.3-2 
 [8] R6_2.2.0          rlang_0.1.1       httr_1.2.1        plyr_1.8.4        dplyr_0.5.0       tools_3.4.0       grid_3.4.0       
[15] gtable_0.2.0      DBI_0.6-1         git2r_0.18.0      withr_1.0.2       crosstalk_1.0.0   htmltools_0.3.6   yaml_2.1.14      
[22] lazyeval_0.2.0    digest_0.6.12     assertthat_0.2.0  tibble_1.3.1      shiny_1.0.3       tidyr_0.6.3       purrr_0.2.2.2    
[29] htmlwidgets_0.8   curl_2.5          mime_0.5          memoise_1.1.0     compiler_3.4.0    scales_0.4.1      jsonlite_1.4     
[36] httpuv_1.3.3     
like image 101
Mike Wise Avatar answered Oct 23 '22 12:10

Mike Wise


At least currently, shapes and annotations are only re-scaled/re-positioned in subplot() if you're using paper coordinates, for example:

rect <- list(
  type = "rect",
  xref = "paper", yref = "paper",
  x0 = 0.5, x1 = 0.6, 
  y0 = 0.5, y1 = 0.6
)
p1 <- layout(plot_ly(), shapes = rect)
subplot(p1, p1)

Feel free to track progress of non-paper coordinates here -- https://github.com/ropensci/plotly/issues/1019

like image 24
Carson Avatar answered Oct 23 '22 11:10

Carson