BookmarkSubscribeRSS Feed

Three Steps to Building a Nested Bar Chart

Started ‎09-14-2020 by
Modified ‎09-14-2020 by
Views 3,358

As we make the transition from summer into fall, I've been seeing lots of news stories describing all the storms that are occurring.  Be it hurricanes, thunderstorms, or just heavy rain it seems that this time of year has a lot of weather events.  This got me thinking, what is the distribution of total weather events throughout the year?  Are there more rainstorms in spring or fall?  To answer this question I decided to create a nested bar chart using the SAS® Graph Builder in SAS Viya.  In just a short time, I was able to create the report below:

 

nestedBarChart_2.gif

 

Very interesting!  The custom graph allows us to see which quarter (or season) of the year specific storm events happen.  More so, since the visual is a nested bar chart, we have the ability to see the number of events for each quarter as a proportion of the total events for the year.  The data source for the example report above comes from a set of csv files that are available on the SAS® Viya® Example Data Sets webpage that contain information about weather events.

 

In this post, I'll show you how to replicate this custom graph and report!  Let's get started:

Step 1: Build the graph

Start by creating a new custom graph using the SAS® Graph Builder.  Drag three bar charts onto the canvas.

 

Next, select the options menu on the left and from the drop down select "Bar Chart 1" and set the "Spacing" option to be 10%. Also choose "Wall Background Color"  for the "Fill Color" option.  Finally select the "Data labels" checkbox:

 

nestedBarChart_SC_01.png

 

Next, still within the options menu, select "Bar Chart 2" and set the "Spacing" option to be 10%. Also set the "Transparency" option to be 67%:

 

nestedBarChart_SC_02.png

 

Next, still within the options menu, select "Bar Chart 3" and set the "Spacing" option to be 10%. Also select the "Data labels" checkbox:

 

nestedBarChart_SC_03.png

 

With the options for all our graph objects in place, we will need to change some options on other pieces of our custom graphs.  From the options menu select "X Axis" and uncheck the "Axis label" checkbox:

 

nestedBarChart_SC_04.png

 

Finally, select the "Discrete Legend" from the main options menu and deselect the check boxes for "Bar Chart 2" and "Bar Chart 1":

 

nestedBarChart_SC_05.png

 

Great!  Now that all of our options edits have been made we can move to creating and editing the graph's roles.  From the menu on the right select the "Roles" menu.  Under the "Shared Roles" section click the three dots next to "Shared Role 1" and choose "Edit Role":

 

nestedBarChart_SC_06.png

 

Rename the role to be "Bar Chart Category" and click "OK":

 

nestedBarChart_SC_07.png

Next, still under the "Shared Roles" section click the "Add Role" button:

 

nestedBarChart_SC_08.png

 

For your new shared role, give it a name of "Bar Chart Response".  Select "Measure" for classification and also select the check box next to "Required".

 

Press OK:

 

nestedBarChart_SC_09.png

 

We will now apply our "Bar Chart Response" shared role to each of the bar charts in our graph.  We'll start with Bar Chart 3.  Under the section for "Bar Chart 3" click the three dots next to the "Bar Chart 3 Response" role and select "Use Shared Role" -> "Bar Chart Response":

 

nestedBarChart_SC_10.png

 

Do the same for Bar Chart 2 and Bar Chart 1.

 

Finally we will need to add an additional role to Bar Chart 3.  Under the section for "Bar Chart 3" click the "Add Role" button:

nestedBarChart_SC_11.png

 

For your new role, select "Group" for the Role Type, name the role "Bar Chart Subgroup" and select "Category" for the classification.

 

Press OK:

 

nestedBarChart_SC_12.png

 

And you're done! You've successfully built the custom graph. Save your graph and give it a name.

 

Step 2: Prepare your data

 

As I mentioned before, the source data for the report shown at the top of this post is data related to storms that have occurred between 2014 and 2018.  This data can be obtained from this webpage which contains several SAS® Viya® Example Data Sets.  However if you do not wish to download data from the SAS® Viya® Example Data Sets site, there is some simulated data in this article's GitHub Repository which can also work in this example.

 

I began by downloading and importing the files us-stormdata-2014.csv, us-stormdata-2015.csv, us-stormdata-2016.csv, us-stormdata-2017.csv and us-stormdata-2018.csv into SAS.  I imported each csv into its own SAS data set (named D_2014, D_2015, D_2016, D_2017 and D_2018).  I decided to use a data step to append these data sets together.  Also for this example, we are only interested in the variables EPISODE_ID BEGIN_DATE_TIME and EVENT_TYPE, so I have used a keep statement to retain only those variables.

 

 

data storms;
length Event_Type $ 50;
set D_2014 D_2015 D_2016 D_2017 D_2018;
keep EPISODE_ID BEGIN_DATE_TIME EVENT_TYPE;
run;
The first few records of the output data set are below:

 

 

nestedBarChart_D_01.png

Next we will need to create some columns to fill all the needed roles for our custom graph.  First we will create a "Year" and "Quarter" variable from the BEGIN_DATE_TIME variable.  This is done with using the datepart function and applying the needed SAS formats to have the values appear as a year and quarter (respectively).  We will want the EVENT_TYPE variable to look a bit nicer in our report, so we can apply the propcase function to this variable and give it a label of "Event Type".  Finally we will need a counter variable so we can summarize all the events for each period.  So we'll create a column with the value of 1 and name it "totalEvents".  We'll apply a label of "Total Events" for this column and also format it with the comma8. format.  The code to do this is below:

 

 

proc sql;
create table storms_nestedBarChart as select distinct EPISODE_ID, datepart(BEGIN_DATE_TIME) as Quarter format=QTR6.,
datepart(BEGIN_DATE_TIME) as Year format=year4., propcase(EVENT_TYPE) as Event_Type label="Event Type",
1 as totalEvents label="Total Events" format=comma8.
from storms;
quit;

 

The first few records of the output data set are below:

 

nestedBarChart_D_02.png

 

Great! Our data is now ready for SAS Visual Analytics!

 

Step 3: Build the report!

Begin by creating a new Visual Analytics report and adding the data set 'storms_nestedBarChart' to your report.  We will first need to set a few data options to our columns.

 

Let's begin importing the custom graph to the Visual Analytics report we're working on.  Drag the custom graph into the report's canvas and apply the data roles as shown:

 

nestedBarChart_R_01.png

 

We can now see our custom graph! 

 

nestedBarChart_R_02.png

 

All that's left is for us to add some window dressing.  In order to make the graph more visually appealing I decided to assign specific colors to the graph.  To do this, select the graph's "Options" menu and scroll down to the "Style" section and change the colors in the "Fill" option:

 

nestedBarChart_R_03.png

 

For the example report in this article, I used the following colors (in the following order):

 

  • F8F9FA
  • C7E3F9
  • C7E3F9
  • 7DC3FC
  • 33A3FF
  • 2C8AD6

 

nestedBarChart_R_04.png

 

Great!  Those colors really make the graph look good!  The last thing we want to add is a little interactivity.  For this, I added a drop down control to my report as a page prompt.  For the drop down's Category role, I placed the "Event Type" variable.  There are a total of 64 distinct values in the "Event Type"  variable.  I think that is a bit much for a drop down selection, so I decided to apply a rank to the variable as shown below: nestedBarChart_R_05.png

 

And that's it! You have successfully built the Nested Bar Chart Custom graph and used it to visualize the storm data in SAS Visual Analytics!

 

nestedBarChart_2.gif

 

How to make this example work for you

This example was created in SAS Visual Analytics 8.5.  The data from the report above can be obtained from the SAS® Viya® Example Data Sets webpage.  However, if you do not wish to extract the data from the SAS® Viya® Example Data Sets webpage, there is some simulated data in our Visual Analytics Custom Graphs GitHub Repository which can also work in this example.

 

On GitHub, you will find the following support files for this article:

  • Five simulated data sets of weather event data - d_2014.sas7bdat, d_2015.sas7bdat, d_2016.sas7bdat, d_2017.sas7bdat d_2018.sas7bdat
  • The code to which creates the final storms_nestedBarChart data set - storms_nestedBarChart_ETL.sas
  • The completed output data set (sourced from the simulated data sets) - storms_nestedBarChart.sas7bdat
  • A JSON file containing the completed custom graph - nestedBarChart_CG.json
  • A JSON file containing the completed report - nestedBarChart.json

 Take Me to GitHub!

 

 

 

Comments

The nested bar chart is interesting. It contains the same information as a stacked bar chart, but enables you to compare counts per quarter (such as Q2 vs Q4) more easily than in a stacked bar chart.

Very interesting and much helpful

Version history
Last update:
‎09-14-2020 03:45 PM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags