1. Efficient visualizations with layouts
Leveling up from our previous videos on combining multiple curves in a single plot, let's explore plot layouts to showcase multiple plots side by side!
2. Layouts
While combining multiple curves in a single plot can often be helpful, it can also obscure the information we want to convey.
In those cases, splitting the curves into subplots and displaying them side by side in a grid is often recommended. Plots-dot-jl simplifies this process significantly. Let's explore how it can be achieved!
3. The grid
Let's begin by loading the streaming DataFrame from a CSV file.
Next, we'll generate a density plot that visualizes the age of survey respondents categorized by their frequency of listening to K-pop.
To display these plots in a two by two grid, we can set the layout argument to 4. Plots-dot-jl will automatically compute an appropriate grid size for our figure.
4. Customizing grid elements
Let's explore how we can customize the elements within the grid individually.
We begin by loading the Julia logo colors from the Colors-dot-jl package and defining a row vector called colors containing the logo colors.
We set the linecolor argument of the density plot from before to the colors vector, which sets the line colors of each grid element, traversing columns first.
5. Controlling the grid layout
We can specify both grid dimensions by passing a tuple instead of an integer to the layout argument. Here, our grid has four rows and a single column.
To set the x-axis labels, we pass a row vector, setting an empty string label for the first three subplots to omit those labels. For the y-axis labels, we pass a string so it applies to all subplots.
To ensure standardized axis limits, we utilize the xlims and ylims functions passing the desired bounds, which are applied to all subplots in the grid.
6. Advanced layouts
Plots-dot-jl also supports advanced layouts, allowing for variations in the number of plots per row. Here's an example showcasing two rows: one containing two subplots, and another containing three.
7. Step-by-step
To construct the plot step-by-step, we first set the theme and define the colors to be used.
Then, we construct the first box plot representing the age of respondents versus the streaming service used.
Next, we group the data by streaming service and set the colors for each group.
We also hide the legends, set the y-axis label, and remove any outliers in the data.
Finally, we assign our plot to the variable p1.
8. Step-by-step
We construct the second box plot similarly but now with the self-reported anxiety levels instead of age. We assign it to the variable p2.
9. Step-by-step
We then create histograms for the age columns grouped by streaming service.
To arrange the histograms side-by-side in a row, we set the layout to a tuple of (1, 3).
We set the axis labels to show the y-axis label for the first plot only. This figure is assigned to the variable p3.
10. Joining the plots
We can quickly join the three plots we've just created.
First, we define a layout using the @layout notation, followed by a representation of the desired arrangement using arbitrary symbols enclosed in square brackets, with rows separated by semicolons.
Having the layout, we call the plot function, passing the three previously generated plots as arguments. Additionally, we assign the constructed layout to the layout argument.
11. Let's practice!
Having control over the grid in plot layouts is a powerful ability, but it also comes with great responsibility. It takes practice and repetition to avoid any unexpected pitfalls. Perfect time for exercises!