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 -- the below shows the same top 2 box data, but only for the top 4 brands in the awareness table:
Requirements
- A visualization with:
- A table used as the Data Source input
- A type that uses the standard Visualization Options for the Data tab.
- This includes: most standard visualizations such as Bar, Column, Pie, Radar, Line, Scatter, Bubble, Pictograph Bar, Geographic Maps, Area, Heatmap, Time Series with Dynamic Window, Palms, Venn, Table with Custom Formatting, and Paste or Enter table.
- This excludes: Visualizations "with Test" in the name, Number visualizations, Mekko, Bar with Skews, Stacked Column with Custom Tests, Column with Trend Tests, Time Series Column, Snake, Scatterplot Matrix, Categorizable Histograms, Correspondence analysis, Single Icon Pictograph and Repeated Icon Pictograph, Conditional Image, Text Analysis, Ranking (Bump) plot, Sankey, Price Sensitivity Meter, Choice Modeling and MaxDiff Diagnostics, Regression and Machine Learning Diagnostics, Table of Means, Brand Health Table, and Correlation Matrix.
- 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 table categories were 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 from the toolbar.
- Click on the page to place the calculation object.
-
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. Paste the code into the R Code editor.
names(table.Awareness)[1:4] - Click Calculate. You'll see an output similar to the following:
- 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