R Data Visualization Recipes
上QQ阅读APP看书,第一时间看更新

How it works...

After loading plotly, the whole figure is stored in the object marg_plot. It uses subplot() function to arrange the plots into a grid. First inputs shall account for the plots to fit into the grid. Order here does matter, the first plots inputted shall fit the top spaces into the grid, last ones shall fit the bottom.

Following this orientation, the first plot inputted was the upper margin histogram (top-right). Next one was an empty plotly plot created with plotly_empty() function. Subsequently came the scatter plot and only then the left margin histogram. Once all these plots were inputted into subplot(), argument nrows is deciding how many rows the grid will have and indirectly the number of columns.

Next, the arguments heights and widths took care of the proportional sizes to be displayed by the columns and rows. Parameter margin brought the plots closer to each other, while shareX and shareY were set to T (TRUE), thus all all the plots were sharing the same axes.

Plot was then called using the layout() function. The parameters picked by this function had the legends removed and bars were set to overlay each other. Similar to grid.arrange(), subplot() is able group any kind of plots crafted with plotly, even plots made with subplot() can be inputted, be creative.