BookmarkSubscribeRSS Feed

3 Steps to Building an Air Temperature Circle Graph

Started ‎01-31-2020 by
Modified ‎01-30-2020 by
Views 5,312

What's your ideal outdoor temperature to go for a jog? For me, I think the best temperature to go running is 65 degrees Fahrenheit. Ever since winter began I've been wondering when we are going to start seeing some 65 degree days. This got me thinking, what's a good way to use SAS Visual Analytics to visualize the average daily air temperatures throughout the 4 seasons of the year?  Specifically, how many days are above and below 65 degrees?

 

At first I considered creating a simple line chart of average daily temperatures, but that didn't show the data the way I wanted. I specifically wanted to show how temperatures swayed by season within one year. With this on my mind I began looking though some of the examples created by SAS Author and Graphing Guru: Robert Alison.

 

I came across this plot which shows sales data by day and year. This method of displaying a year's worth of daily data on a circle instead of a flat line is fantastic! I began to wonder if I could build something similar for use in SAS Visual Analytics. I quickly opened the SAS Graph Builder and in just a few minutes, I was able to create the following graph:

 

airTempCircleGraph_600.png

 

 

The report above uses air temperature data from the USGS Water Services website (Credit: U.S. Geological Survey - Department of the Interior/USGS). I downloaded data from a site located on Kerr Lake (site number: 02079490). The graph contains a circle with several lines stemming from it. Each line represents the average air temperature for each day of the year. The blue lines that extend inward represent days where the average temperature was BELOW 65 degrees. Conversely, the red lines that extend outward represent days where the average temperature was ABOVE 65 degrees. The length of the lines represent the actual value of the average air temperature for each day.

 

 Pretty neat! In this post, I'll outline the three steps to add these types of labels to recreate the report above. Let's get started!

 

Step 1: Build the graph

 

Start by creating a new custom graph using the SAS® Graph Builder.  Drag the following objects onto the canvas in the following order:

  1. Scatter Plot
  2. Vector Plot
  3. Vector Plot
  4. Scatter Plot

Now select the options menu on the left and from the drop down select "Scatter Plot 2" and under the "Marker style" section, change the size of the markers to be 5:

 

GC_01.png

 

Next use the drop down menu to select "Vector Plot 2" and clear the "Show arrowheads" checkbox:

 

GC_02.png

 

Now make the same change to the other vector plot. This is done by selecting the options menu on the left and from the drop down select "Vector Plot 1" and clear the "Show arrowheads" checkbox.

 

We'll continue our graph objects editing by selecting "Scatter Plot 1" and under the "Marker style" section, change the size of the markers to be 1:

 

GC_03.png

 

Now, still under the main "Options" menu, select "X Axis" from the drop down and de-select all check boxes. Also set the "Grid lines:" to "Off":

 

GC_04.png

 

Make the same changes to the "Y Axis".

 

Still within the options menu, select the "Legends" section from the drop down and set the Visibility to "Hide All":

 

GC_05.png

 

Finally, in the "Discrete Legend" section of the options menu uncheck all the boxes next to the report objects names:

 

GC_06.png

 

Great! Now that our graph options are set, we need to make some edits in the "Roles" menu.

 

Start by selecting the main "Roles" menu and next to the "Shared Role 1" role click the three dots and click "Edit Role":

 

GC_07.png

 

 

Rename the role to "Label Location X".  Also click the "Unshare" button next to both the "Scatter Plot 2 X" and "Vector Plot 2 X".  When you are finished the screen should look like this:

 

GC_08.png

 

Press OK.

 

Next scroll down to the "Vector Plot 1" section and click the three dots next to the "Vector Plot 1 Y" role. Choose "Create Shared Role With Another Role" -> "Scatter Plot Y":

 

GC_09.png

 

At the next screen, name the role "Label Location Y".  Press OK.

 

Now scroll up to the "Scatter Plot 2" roles and click the three dots next to the "Scatter Plot 2 X" role. Choose "Create Shared Role With Another Role" -> "Vector Plot 2 X Origin":

 

GC_10.png

 

 

At the next screen, name the role "Baseline X".  Press OK.

 

Make a similar edit to the "Scatter Plot 2 Y: role. Only this time choose "Create Shared Role With Another Role" -> "Vector Plot 2 Y Origin".  At the next screen, name the role "Baseline Y" and press OK.

 

Now under the "Vector Plot 2" roles, click "Add Role":

 

GC_11.png

 

At the next screen, select "Color" for the role type and input "Color" for the Role Name. 

 

Press OK:

 

GC_12.png

 

Finally, scroll down to the "Scatter Plot 1" roles and click "Add Role":

 

GC_13.png

 

At the next screen, select "Data Label" for the role type and input "Label" for the Role Name. 

 

Press OK:

 

 

GC_14.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 can be obtained from the USGS Water Services' website (Credit: U.S. Geological Survey - Department of the Interior/USGS). However if you do not wish to download data from the USGS Water Services' website there is some simulated data attached in this post's supporting GitHub Repository which can also work in this example.

 

After importing the downloaded data into SAS, I converted the water temperature readings to Fahrenheit. After this conversion the data looks like this:

 

D_01.png

First we will want to get the average temperature for each day. This can be done with a simple PROC SQL step:

 

libname src '<-location of source data>';

proc sql; create table dailyAVGTemps as select Date, avg(Air_Temp) as avg_air_temp format=best4.0 from src.airTemps group by Date; quit;

 

The next step is to set the 'baseline' for what we want our 'baseline' temperature to be. For the example in this post, I have used a temperature of 65. We will save this in the macro variable "baseline".

 

Now we will need to add a new column (named tempDiff) to show how many degrees each day's average temperature was above or below our baseline. Also we will want to create a "DayOfYear" column to tell us which numeric day (between 1 and 365) each day lands. The code to do this is below:

 

%let baseline = 65;

data formatData; set dailyAVGTemps; tempDiff = (round(avg_air_temp - &baseline)); DayOfYear = Date - intnx('year',Date,0)+1; run;

 

The next step is to create coordinate data points for the x and y positions for each day's average temperature to be set in our vector plot. This is very similar to how an annotate data set is used when creating ODS graphs.  In order to generate these positions, we will join our data set 'formatData' to the "CircleTemplate" data set which is in this post's supporting GitHub Repository.  The join will be based on both "DayOfYear" and "tempDiff".  The code to do this is below:

 

proc sql;
create table joinToTemplate as select
t1.*, t2.*
from formatData as t1
left join src.CircleTemplate as t2
on (t1.dayofyear = t2.dayofyear) and (put(t1.tempDiff,best3.0) = put(t2.tempDiff,best3.0));
quit;

 

After completing this join, the output data set "joinToTemplate" looks like this:

 

D_02.png

 

The final step is to create the lines and labels and locations for the text Jan, Apr, Jul and Oct.  We will use put and substr functions to place these at the first of each of these respective months.  In order to determine how far away from the circle to place our labels we will do a simple calculation of plus or minus a percentage of the distance that the circle or vector points are from the center.

 

Also we will create a "color" variable to indicate whether the average daily temperature was above, below, or equal to our baseline.  The code to do this is below:

 

 

data AirTempCircleData;
set joinToTemplate;
if substr(put(date,date9.),1,5) = "01JAN" then LabelX = 0;
else if substr(put(date,date9.),1,5) = "01APR"  then LabelX = x_base+(x_base*.20);
else if substr(put(date,date9.),1,5) = "01JUL"  then LabelX = 0;
else if substr(put(date,date9.),1,5) = "01OCT" then LabelX = x_base+(x_base*.25);

if substr(put(date,date9.),1,5) = "01JAN" then LabelXOrigin = 0;
else if substr(put(date,date9.),1,5) = "01APR"  then LabelXOrigin = x_base;
else if substr(put(date,date9.),1,5) = "01JUL"  then LabelXOrigin =  0;
else if substr(put(date,date9.),1,5) = "01OCT" then LabelXOrigin = x_base;

if substr(put(date,date9.),1,5) = "01JAN" then LabelYOrigin = y_base;
else if substr(put(date,date9.),1,5) = "01APR" then LabelYOrigin = 0;
else if substr(put(date,date9.),1,5) = "01JUL" then LabelYOrigin = y_base;
else if substr(put(date,date9.),1,5) = "01OCT" then LabelYOrigin = 0;

if substr(put(date,date9.),1,5) = "01JAN" then LabelY = y_base+(y_base*.20);
else if substr(put(date,date9.),1,5) = "01APR" then LabelY = 0;
else if substr(put(date,date9.),1,5) = "01JUL" then LabelY = y+(y*.15);
else if substr(put(date,date9.),1,5) = "01OCT" then LabelY = 0;

if substr(put(date,date9.),1,5) = "01JAN" then Label = "Jan";
else if substr(put(date,date9.),1,5) = "01APR" then Label = "Apr";
else if substr(put(date,date9.),1,5) = "01JUL" then Label = "Jul";
else if substr(put(date,date9.),1,5) = "01OCT" then Label = "Oct";

XscatterLabel = LabelX;
YscatterLabel = LabelY;

length color $ 10;
if avg_air_temp gt &baseline then color = "Above";
else if avg_air_temp lt &baseline then color = "Below";
else color = "Equal";

baselineTemp = &baseline;

run;

 

A subset of the final output data is below:

 

D_03.png

 

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

 

Step 3: Build the report!

 

All that's left for us is to import the custom graph to a Visual Analytics report. 

 

Add your new custom graph and the data set 'AirTempCircleData' to your report and apply the roles as shown:

 

R_01.png

 

 

You can now see the graph! 

 

R_05.png

 

Depending on the type of data you're graphing, you'll probably want to change the colors of the graphs features.  Since this post's example is about cold or hot days, I chose to make the colors blue and red.  To do this, click the graph to select it and go to the options menu. Select the graph's colors to be whatever you would like:

 

R_02.png

 

The HEX values for the colors I chose are below:

 

  • Color 1: 287FC7
  • Color 2: B0B7C2
  • Color 3: DD5757
  • Color 4: 33A3FF
  • Color 5: 000000

 

 

All that's left for us to do is add some window dressing.  For this, I created a custom legend from a text object and placed it under the graph. I also created and applied a custom theme to make the month's name labels appear bigger. The custom theme and final report are included as JSON files in this GitHub Repository.  Additionally, I applied a fixed report size of 700x700 to the report to ensure that the graph always renders in a square.  After these edits, our final report looks like this:

 

airTempCircleGraph_600.png

 

 

Congratulations!  You have successfully built the Air Temperature Circle graph for use in SAS Visual Analytics!

 

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 USGS Water Services' website (Credit: U.S. Geological Survey - Department of the Interior/USGS). However, if you do not wish to extract the data from the USGS Water Services' website, there is some simulated data in our Visual Analytics Custom Graphs GitHub which can also work in this example.

 

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

  • A simulated data set of air temperatures - simulatedairtemps.sas7bdat
  • The code to which creates the final airTempCircleData data set - airTempCircleGraph_ETL.sas
  • The completed output data set (sourced from the simulated data set) - airtempcircledata.sas7bdat
  • A JSON file containing the completed custom graph - AirTempCircleGraph_CG.json
  • A JSON file containing the completed report - AirTempCircleGraph.json
  • The theme used to create the large text labels - AirCircleTheme.td

 Take Me to GitHub!

 

Import the data on your SAS Visual Analytics instance.  Import the report via the "Import via GUI" section of these instructions.  

Comments
BJM

Unfortunately, we don't have SAS Viya.

Is there a way to do it in SAS Graph or ODS graphics (maybe SGPlot)?

 

Hi BJM!

 

I got the inspiration for this graph from this example by Robert Alison which uses SAS/Graph.

 

If you follow the link to the example you will find some SAS code that creates a similar plot using SAS/Graph.  Using the example SAS code as a starting point, you could make any modifications you would like to make the graph look the way you want!

 

Cheers!

 

- Mike

Sanjay Matange shared an ODS graphics example on his blog.  https://blogs.sas.com/content/graphicallyspeaking/2012/04/09/simpler-is-better/

BJM,

You can also create plots that use polar coordinates by using GTL or the SG procedures. For one example, see this blog post.

For a slightly different approach, see the article "Create a deviation plot to visualize values relative to a baseline."

 

Mike,

Just curious, but why choose clockwise as the direction of time instead of counter-clockwise?  Convention is to have things turn counter-clockwise.  Clocks are an anomaly.

Hi Chris!

 

Going clockwise was simply my first inclination when I was initially creating this custom graph. But going counter clockwise is a also great idea! This example could certainly be modified to go counter-clockwise if that's the direction you would prefer!

 

That's the best part of SAS Communities: where one idea triggers another one!

 

Mike

Version history
Last update:
‎01-30-2020 08:40 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