Joining charts

In this vignette we will show how to join charts in different ways. This is a useful functionality of the tidycharts package, as it enables the user to group charts into panels or even generate multiple charts by one command (facetting).

library(tidycharts)

Joining charts

Combining plots in one panel is useful and easy by using the function join_charts. By setting nrows and ncols parameters user can control the layout of the grid.

As you can see in Getting Started vignette, we created grouped bar chart and two variance bar charts. We can use the data and plots from that example to create a joined chart.

join_charts(
  bar_chart_grouped(data = data_barchart,
                  cat = data_barchart$dep,
                  foreground = 'prev_year',
                  background = 'profit',
                  markers = 'plan',
                  series_labels = c('PY', 'AC', 'PL')),
  bar_chart_absolute_variance(cat = data_barchart$dep, 
                                baseline = data_barchart$plan,
                                real = data_barchart$profit,
                                y_title = 'Plan vs. actual',
                                y_style = 'plan'),
  bar_chart_relative_variance(cat = data_barchart$dep, 
                                baseline = data_barchart$plan,
                                real = data_barchart$profit,
                                y_title = 'Plan vs. actual',
                                y_style = 'plan'),
  nrows = 1,
  ncols = 3
) %>% SVGrenderer()

Facetting

Facetting means dividing data into some categories and generating charts for each category. We will apply facet_chart function to visualize data from R built-in demo dataset mtcars.

head(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

We should facet the data by a variable which doesn’t have many unique values (preferably a categorical variable), so we won’t end up with astounding number of charts. In this case, we will facet by cyl variable, that has only 3 unique values, so we will get 3 charts.

We need to pass FUN argument to facet_chart function, which must be one of plotting functions in the package. FUN is responsible for creating base charts. Arguments to FUN can be passed through .... In the example, scatter_plot is used.

facet_chart(data = mtcars, 
            facet_by = 'cyl',
            ncols = 2,
            FUN = scatter_plot,
            x = mtcars$qsec,
            y = mtcars$hp,
            cat = mtcars$gear,
            legend_title = '',
            x_names = c('1/4 mile time', 'is s'),
            y_names = c('Horsepower', '')) %>% 
  SVGrenderer()