Fine-tuning the date axis in your bar chart

In these modern times, my utility bill has a bar chart so I can see how my current consumption compares to my historical usage. I decided to create my own version of this utility bill bar chart, and make a few improvements along the way. If you're into SGplot bar charts, you might learn a thing or two from this example!

But first, you need to guess what kind of utility data I'll be plotting. Is it water, sewer, power, or gas?!? Here's a hint, in the form of a picture my friend Annie took - very cool photo, eh?!?

And now, let's plot my power utility data! I grabbed one of my old power bills, and created a SAS dataset of the values shown in their graph. They don't give a specific date (just the month), but I went ahead and called it the first of the month in my dataset, to make it a valid 'date' value ...

And with the code below, here's my first bar chart of the data. Notice that I use the monyy7. date format, so the dates just show the month and year (not the '01' day part). I also used group=billmonth, to make the current/billing month's bar a different color than the other months.

That's an ~OK plot, but I think the date values along the bottom are a bit difficult to read (since I had to rotate them to make them fit), and having the year repeated so many times seems a bit wasteful of space. So let's apply the monname3. format to make the date values print as the 3-character month abbreviation. Those values will be short enough to fit under the bars without rotating. It looked good at first, but then I noticed that since the plot covers a 13 month time span, the graph has 2 September's (from 2 years) ... and when you plot the bars by the 3-character month abbreviation, the 2 Sep's get stacked into the same bar!

One advantage of the newer SGplot over the old SAS/Graph Gchart is that it has more "time smarts" built into it. Therefore I added the type=time option on the xaxis statement, to let it know to handle the values in a "time smart" way, and voilà - I got a chart with a very nicely formed time axis!

Now that we've got the graph looking great, let's create the rest of the power-bill look. They had a logo and address at the top of the bill - how can you add something like that to a SAS graph?!? ... By using annotate, of course! First, use the pad option to add some blank space above the graph:

I then used the following code to create the annotate dataset, and specified sganno=anno_all to include it in the SGplot bar chart.

Hopefully you've learned a few tricks you can use in your own graphs! ... If you'd like to play around with this graph, here's a link to the complete SAS code. And if you're interested in utilities & energy, and would like to see more blog posts on this topic, click here.

About Author

Robert has worked at SAS for over a quarter century, and his specialty is customizing graphs and maps - adding those little extra touches that help them answer your questions at a glance. His educational background is in Computer Science, and he holds a BS, MS, and PhD from NC State University.

About

Welcome to Graphically Speaking, a blog focused on the usage of ODS Graphics for data visualization in SAS. The blog will cover topics related to the Statistical Graphics procedures, the Graph Template Language and the ODS Graphics Designer.

For more in-depth information on using the ODS Graphics system, please check out the following SAS Press books :