Yes Polygons


 Yes Polygons

In October of 2018, when I had been using Tableau for approximately 6 months, I launched KevinFlerlage.com.  The first two posts (Filled Shape Charts and Sweet Take on IronViz) were ones that I originally wrote as guest blog posts on KenFlerlage.com and decided to add to my website.  The third post was a video that I created regarding my excitement for the conference (this was a fun one).  The next post was truly the first blog post that I had written specifically for my website.  That blog post was about a technique that I created which became known as No Polygons.  It was published on October 10, 2018 – which is almost exactly one year prior to the publishing of this blog post. 

No Polygons immediately took off.  The tweet itself received hundreds of likes and dozens upon dozens of retweets.  I even recall Johnathon Drummey commenting on how it was a creative solution to a tricky problem.  Wow!  But then, the vizzes started rolling in.  Since that time, I’ve literally witnessed hundreds of visualizations built using the No Polygons technique (the vast majority of these vizzes were petal charts done in small multiples).  Five of them (at least that’s my count) even received Viz of the Day honors.  I would have never expected that something I developed just six months into this journey would have become so popular.  I am incredibly grateful and this blog post will forever be incredibly special to me.

The following is a fantastic example of a viz created by Sarah Burnett using No Polygons:


The truth is – as I clearly stated in my blog post – No Polygons was developed as a way to circumvent using polygons (hence the name) due to, at that time, my firm position as a newbie with a colossal fear of using actual polygons.  I no longer fear polygons, nor have I for some time.  However, I know that other people do, so I plan to help them face the fears that once haunted me as well.  In this blog post, I will break down how to create visualizations similar to the ones people have created with No Polygons, but to do so using the very technique that I once avoided.  I present to you Yes Polygons. 

Disclaimer

Thousands of people have created visualizations with polygons and many of them have written about them.  However, with this blog post, I simply want to give my perspective as well as compare and contrast it with the No Polygon technique.  At the bottom of the blog post, I include numerous links to other techniques.  

No Polygon Refresher

I would encourage you to first read and understand what the No Polygons technique is.  However, as a brief refresher, No Polygons is a way of using a variety of custom shapes, all placed in certain locations on the same size palette.  The shapes with palette are stacked on top of each other to create one image that fits together perfectly.  Below is a visual example of what I am talking about:



The four images above are all laid on the same-sized transparent palette then are stacked on top of each other to create the following image.



Shapes in a Tableau viz are sized around the center point and since all of the shapes shown above share a center point; they can be sized based on a measure and still meet up at that center point.  There is much more to the No Polygon technique, but this is a general refresher.  I certainly encourage you to read the entire blog post. 

Pros & Cons of No Polygons

The advantages of No Polygons are that it requires no math and no knowledge of drawing polygons.  You do not have to duplicate your data in any way, shape or form.  And most importantly, you can use any shape you want – you can use shapes with curves, jagged sides, even use actual photographs (which I did on my Blossoming Countries viz).  As long as you can construct a way for the individual “petals” to meet at a common center point on a common palette, then you can use the technique. 

There are, however, several disadvantages as well, many of which were noted in my original blog post.  With No Polygons, you must stack images on top of each other.  This vastly reduces your ability to implement dashboard actions as you can really only interact with the shape that is on top (my original blog post suggested a dual axis with transparent shape on top).  This also hinders your ability to utilize tooltips and you are forced to create calculations to show the tooltip for all petals when you hover over the top shape. 

Polygons

Polygons are just shapes with three or more sides.  They can be regular (equilateral – all sides are the same length) or irregular (sides are of different lengths).  Polygons are essentially shapes and if you can calculate the points or corners of those shapes, you can draw polygons in Tableau. 

Pros & Cons of Polygons

Using polygons, we can create similar small multiple petal charts like the example above, but like No Polygons, they come with advantages and disadvantages.

Polygons require no images.  You determine the points on the polygon, typically using math, then draw those points in Tableau.  Each petal is its own, unique polygon, so you can interact with the viz as you normally would.  You can implement dashboard actions to work with each individual petal and you can see tooltips for each part, not everything mashed together like with No Polygons. 

However, polygons typically require mathematics, often geometry and trigonometry, to determine the points needed to draw each polygon (I personally love this part).  Straight lines are typically easy, but curved lines become much more difficult.  You are also limited to what you can draw – for example, it would be very difficult to draw a true flower petal like in Blossoming Countries. Polygons usually require the use of table calculations (typically just Index, but you still need to have some understanding).  Lastly, polygons will require that you duplicate your data.  (Note: use caution any time you duplicate your data.  Duplicating a data set of 100 million records will not be a good idea.  If your data set is large, I would recommend that you aggregate your data before utilizing this method...or simply use an alternative method like No Polygons).  

Ultimately, the use case will most likely dictate which technique you use.

How Do We Use Polygons

In late August, Christian Felix shared a beautiful ProjectHealthViz called The Shape of HealthcareSpending.  The viz utilized the No Polygons technique.  In this blog post, I will explain how to recreate this visualization using Polygons instead of No Polygons.

Before moving on, I would encourage you to look at Christian's incredible viz. 


Christian (who is a great guy by the way) uses four squares to visualize healthcare spending for four values: medical goods, administrative, rehabilitative, and preventive.  He adds a nice shadow effect to the shapes and he lays out the four squares to share a center point. The squares are colored to associate with each value then are sized based on the volume of spending.  Lastly, he lays it out in small multiples for each country.  A single country looks similar to the following where rehabilitative (green) shows the highest volume of spending followed by medical goods (yellow), then preventative (blue), and administrative (red).



Okay, in order for us to draw polygons, we will need to draw all four corners of each of these squares then simply change the mark type to polygon.  But how do we determine the coordinates of these points (16 in total)?  In most cases, we will use mathematics to determine the points so that it’s very repeatable whether there are four sides or 100, but in this case, we are going to keep it very simple and just figure out the values for each of the 16 points.  (Please feel free to follow along in my visualization where you can access both Christian’s original viz and my remake using polygons.  Please note that in my workbook, I have added the @ symbol to all my calculations so that they are easy to find). 

For my example, I am going to plot these four squares on an X Y axis with the squares all meeting in the center at X Y coordinate of 0, 0.  For this exercise, I am also going to set the size of all four squares to be identical, all measuring 1 X 1.  I’m also going to lighten the color a bit. 



In addition to this, I am going to label the four points we need to draw on each square.  It will follow the same path as shown below on a single square. 



Okay, hold up there partner!  How are we going to draw four points for one “value” in our data?  Well, we need to duplicate our data.  In this case, we need four points so we need four sets of this same data.  We can do this using data densification, but for this example, I’m going to simply union my data on top of itself four times.  For more information on union, please see Union your Data.

Once we have done this, Tableau will have created a calculation called TableName.  This will show us the four different tables we unioned together.  For simplicity, I created a calculation that renames these tables to Point1, Point2, Point3, and Point4 as each table will be used to create one point.  I called the calculated field “Point #” and used the following case statement: 

CASE [Table Name]
    WHEN 'OECD_Data'  THEN 'Point1'
    WHEN 'OECD_Data1' THEN 'Point2'
    WHEN 'OECD_Data2' THEN 'Point3'
    WHEN 'OECD_Data3' THEN 'Point4'
END

Okay, let’s determine the X coordinate only for each point in the green square.  

For point 1, what is the X coordinate?  Well, the X coordinate is simply 1.  For point 2, our X coordinate is 0.  For point 3, it is also 0.  And for point 4, the X coordinate is once again 1.  That was simple, right?  Let’s do the same thing for the yellow square.

X Coordinate for Yellow Square:
          Point 1 = 0
          Point 2 = -1
          Point 3 = -1
          Point 4 = 0

Okay, this is easy.  Let’s finish it off:

X Coordinates for Blue Square:
          Point 1 = 1
          Point 2 = 0
          Point 3 = 0
          Point 4 = 1

X Coordinates for Red Square:
          Point 1 = 0
          Point 2 = -1
          Point 3 = -1
          Point 4 = 0

Okay, easy peasy!  What about the Y coordinates?




Y Coordinate for Green Square:
          Point 1 = 0
          Point 2 = 0
          Point 3 = 1
          Point 4 = 1

Yellow:
          Point 1 = 0
          Point 2 = 0
          Point 3 = 1
          Point 4 = 1


Blue:
          Point 1 = -1
          Point 2 = -1
          Point 3 = 0
          Point 4 = 0

Red:
          Point 1 = -1
          Point 2 = -1
          Point 3 = 0
          Point 4 = 0

Okay, we figured out the points, now all we need to do is write a calculation to compute them using our data.  Before we do that, let’s create a calculation to allow us to relabel the dimensions so that visually, it makes more sense.  All I want to do is to label each square with a color.  I called the calculation “Square Color”.

CASE [Metric1]
    WHEN 'Curative and rehabilitative care' THEN 'GREEN'
    WHEN 'Medical Goods' THEN 'YELLOW'
    WHEN 'Preventive Care' THEN 'BLUE'
    WHEN 'Administration' THEN 'RED'
END

Okay, now it’s time to write the calculations for the X coordinates and the Y Coordinates.  However, above we used consistent side lengths of 1 for each side.  We do not want to do that, we want to size the polygons based on measures in our data.  Christian uses a measure called “Percent” and sizes the squares based on this measure. 

You should know that Tableau sizes shapes (squares, circles, etc.) based on area.  So the area of each square in Christian’s viz is equivalent to the “Percent” value.  The area of a square is calculated by multiplying the length by the width.  The length and the width are the same so calculating the area for a square is just the length squared. 

I tell you all of this because the polygon approach hinges on our ability to measure length, not area (remember above, we were calculating x and y values which were simply lengths of sides).  So we need to find the length of a side based on the area.  Since the area = length squared, the length would equal the square root of the area.  So create a calculation called “Side Length”:

SQRT([Percent])

Now, we will replace the lengths of 1 that we used above with this Side Length calculation (which will be different for each square).  Therefore 1 will become Side Length and -1 will become –Side Length (you will retain that negative sign).  Zeros will remain zeros. 

We will use a case statement that first looks at the points for the green square, then the yellow square, etc.  We will break down the coordinates for each point in the square before moving on.  Call the calculation “X Coordinate Polygon”:

CASE [Square Color]

    WHEN 'GREEN' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN [Side Length]
            WHEN 'Point2' THEN 0
            WHEN 'Point3' THEN 0
            WHEN 'Point4' THEN [Side Length]
        END)

    WHEN 'YELLOW' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN 0
            WHEN 'Point2' THEN -[Side Length]
            WHEN 'Point3' THEN -[Side Length]
            WHEN 'Point4' THEN 0
        END)

    WHEN 'BLUE' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN [Side Length]
            WHEN 'Point2' THEN 0
            WHEN 'Point3' THEN 0
            WHEN 'Point4' THEN [Side Length]
        END)

    WHEN 'RED' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN 0
            WHEN 'Point2' THEN -[Side Length]
            WHEN 'Point3' THEN -[Side Length]
            WHEN 'Point4' THEN 0
        END)

END

Okay, now we will do the same with the Y coordinate.  Call the calculation “Y Coordinate Polygon”:

CASE [Square Color]

    WHEN 'GREEN' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN 0
            WHEN 'Point2' THEN 0
            WHEN 'Point3' THEN [Side Length]
            WHEN 'Point4' THEN [Side Length]
        END)

    WHEN 'YELLOW' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN 0
            WHEN 'Point2' THEN 0
            WHEN 'Point3' THEN [Side Length]
            WHEN 'Point4' THEN [Side Length]
        END)

    WHEN 'BLUE' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN -[Side Length]
            WHEN 'Point2' THEN -[Side Length]
            WHEN 'Point3' THEN 0
            WHEN 'Point4' THEN 0
        END)

    WHEN 'RED' THEN
        (CASE [Point #]
            WHEN 'Point1' THEN -[Side Length]
            WHEN 'Point2' THEN -[Side Length]
            WHEN 'Point3' THEN 0
            WHEN 'Point4' THEN 0
        END)

END

Okay, that’s all the calculations we need, so let’s build the viz for just one country. 

  1. Add Country1 to the filter shelf and select only United States.
  2. In his viz, Christian sizes the squares based solely on the most recent year of data, so add a filter of Year and set it to 2017 only.   
  3. Now move X Coordinate Polygon to the Columns shelf and change it to an average. 
  4. Move the Y Coordinate Polygon to the Rows shelf and change it to an average. 
  5. This will yield a single point because everything will be aggregated.
  6. We need to break this out by the colored squares, so add Square Color to detail.  This will create four points in total, one for each square.
  7. However, we also need to break it out by Point #.  Add Point # to detail.
  8. Now we have four points for each of our four squares.
  9. Change the mark type to polygon and you’ll get a pretty design. 
  10. Move Point # over to the Path card in order to tell Tableau the direction in which to draw the polygons (1, 2, 3, and 4). 
  11. Now you should have four blue squares, which is not quite what we want.   So move Square Color onto the Color card to color each square differently.  (Update the colors to match the square color).
  12. Your canvas should now look like the following:



Congratulations, you have successfully built the polygon version of this No Polygons chart using Polygons – well for one country at least.  Now, change the Country1 filter to show only the Latvia and you should see a very different view than with the United States. 

Okay, that is generally it for the building of this polygon petal chart.  Let’s finish off the viz by building out the small multiples.  Luckily, Christian has already done all this work for us.  Simply move Column Selector to Columns and Row Selector to Rows then change each to discrete dimension. 

Finally, I set the both axes fixed at -4 to 4 (to ensure they are square), removed grid lines, and generally cleaned up the look.  I switched the tooltips around so that they are on the polygons themselves rather than on the country header label (one of the advantages of using polygons over shapes via the No Polygons technique).  And that’s it…a small multiple polygons chart!

Wrap Up

These two methods are not the only ways of accomplishing these types of visualizations.  Check out the variety of methods and blog posts from: Steve Wexler, Rody Zakovich, Rajeev Pandey, Rosario Guana and another one from Neil Richards and Workout Wednesday.  I encourage you to check out these different methods in order to compare and contrast them.  You can check out both No Polygons and Polygons by going to my remake viz.  I also encourage you to download Christian’s viz and try to recreate it using Polygons like I instructed above (or using any of these methods).  If you click on the image below, it will provide you with an option to look at both his gorgeous original using No Polygons as well as my remake with Polygons. 


Please note that there are a lot of techniques that involve polygons, most of which involve some level of mathematics.  Ken Flerlage and I will be presenting on this topic at the Tableau Conference 2019 - The "TableauTwins" Take You Beyond Show Me.  In our session, we will provide detailed tutorials on building advanced charts, starting with required fundamentals and then moving to the use of mathematics, data densification, etc. We will demonstrate how to apply these techniques in a variety of charts.  This is intended for intermediate users and there are no prerequisites required; you don’t need to be a math genius or even remember 10th grade trigonometry! We will provide you with everything you need to take your skills from intermediate to advanced.  If you will be at the conference, I hope you can join us. 

A final note, this viz is beautiful and a ton of fun and I ALWAYS encourage others to create different chart types in your personal work.  It is a great learning experience to help you grow, help you to understand Tableau better, and even find techniques you may have not knew of in the past.  However, please don't go swapping out your business bar charts for petal charts because in most uses cases, a bar chart would be a far better choice.

I hope you enjoyed this blog post and as always, if you ever have any questions or comments, please feel free to contact me at any time.


Kevin Flerlage, October 7, 2019 | Twitter | LinkedIn | Tableau Public







No comments

Powered by Blogger.