Guest post from Craig Wortman, who will be presenting tomorrow at the Cincinnati Tableau User Group meeting.
Creating a MLB Bar Code Chart in Tableau with Custom Team Colors
A little over a year ago, Jeff sent me a link to “BallCode” created by Peter Gilks visualizing the 2013-2014 NBA Regular Season. I immediately thought, "this Viz is fantastic" and within a few weeks of discovering it, I had developed a similar-style “BarCode” chart tracking vital performance metrics for work. This “BarCode” chart solution, also known as a "win-loss" chart, is more or less a score sheet for success or failure of certain work initiatives. I found this was a great method for visualizing a certain metric on an executive dashboard.
Given the success of the “BarCode” chart at work, I was intrigued by how this chart could be utilized in other opportunities. While I think it is an awesome Viz tool, the truth is it doesn’t work well in all circumstances. The “BarCode” chart requires a data set with small multiples and a lot of binary results over a long period of time, which makes it perfect for Baseball!
What I'll demonstrate here is what success and failure looks like over the course of a season for every MLB team during the 2015 season. As expected, wins are up and losses are down, and I have coded individual team colors to each of the charts. I did this using custom color pallets, which is a great tip/trick for bringing custom color palettes into your Viz. Tableau has a great explanation of how to do this here. I also have a copy of the color codes for each teams color that you can download here in the format necessary to insert into your Tableau preference file.
The first two pages of the Viz are the same chart in different size formats. I am experimenting with the best way to display this kind of data, and am unconvinced as to which is best. So if you have an opinion, hit me up and let me know! The third and fourth pages are a really cool moving graph showing the race for the playoffs as it develops over the season. The idea of using the dates of the games as a page filter to let you see the race for the playoffs develop over the season is something that was suggested to me, and I think it’s a great addition to the story being told here. It’s also a very simple little design that displays a lot of neat information with it. However, page filter automated controls do not work on Tableau Public, so I have included a GIF version of it on the fourth page so you can see what it looks like in motion. You may need to allow this in your browser security to see it play.
So, the quick and dirty on how to make charts like this. You need the right kind of data obviously. First and foremost, to make a “BarCode” chart you need to have a binary dataset, or data that can be easily grouped into success/failure, yes/no, etc. The notion of a double header makes this a little challenging in my dataset. You can handle this in many different ways, but I chose to simply let those values add up and that shows as a longer bar in a few places. If you had data that skewed too far out on occasion, say 5 victories in one day, it would cause problems with the scale of the chart. This could be addressed by setting a cap on victories per day or making groups of one victory and multiple victories. Either way, definitive good and bad outcomes are best for “BarCode” charts.
Also it helps to have a lot of events. If you only have three or four events with outcomes, the chart will look pretty sparse. So a lot of events, with many of binary outcomes, and more than one team or group per chart is the core of the “BarCode” chart.
The key to making a “BarCode” chart is to make sure the data has a value tied to the outcome: you are representing set to be either a 1 or a -1. (In my example, I allow the data to sum to 2 or -2 in the case of a double header, but you could limit that easily.) In the data I used for this example, I had to create a really simple and quick Calculated Field to make this data.
The only other part that could be considered tricky here is how I have two different colors for each team in the chart. This is handled by making the following calculated field, and then dropping that into the color box for the bars.
Feel free to download the workbook from Tableau Public to see exactly how I assembled and created this viz.