This article describes how to use a Calculation to filter and order a visualization. This can be useful when you are doing a lot of filtering in your document and have different brands for different demographics that you'd like to show. Similarly, you can use this approach to order rows/columns in a visualization based on the rows/columns of a separate input.
This example walks you through how to go from a visualization ordered as the input table is - this is showing top 2 boxes for cola brands:
To a state where only certain bars are shown and in a particular order -- below shows the same top 2 box data, but only for the top 4 brands in the awareness table:
Requirements
Please note this requires the Data Stories module or a Displayr license.
- A visualization without tests based on a table of data (rather than variables). See How to Create a Column Chart from a Table.
- A Calculation listing the rows/columns to show in that order in the visualization. Note, the names must match the table used to create the visualization exactly.
Method
For my example, I will use the following table as an input to my visualization.
I will also use the following awareness table in my Calculation. The categories in the table was sorted using a Rule. See How to Automatically Sort Table Rows.
- After creating your visualization, you need to create a Calculation that lists the rows/columns you want to show in that particular order. To do so go to Calculation () > Custom Code.
- Create your own custom calculation to create a series of the row or column names you'd like to show in that order. My code for this example is below. It uses the Awareness SUMMARY table above and pulls off the top 4 brands in order.
names(table.Awareness)[1:4]
- Click Calculate. You'll see an output similar to below:
- Now we can use this Calculation to filter/order our visualization:
- Select your Visualization.
- In the Object Inspector, go to Data > Row Manipulations > Select rows to show by > Calculation output.
- In the Rows to show field, select or type in the Name of your Calculation made in Step 1.
- [OPTIONAL] To do this for your columns you'd use: Data > Column Manipulations > Select columns to show by and Columns to show.
Next
How to Filter Rows and Columns in Visualizations and Tables Without Code
How to Select Which Rows or Columns to Show in a Visualization Based on a Control Box Selection
How to Use Point and Click Inside R Code
How to Reference and Distinguish between Different R Objects in Displayr