Traversing Color Gradients
I remember when I learned in high school painting many years ago that the sky wasn't a consistent shade of blue across its vast reach to the human eye, but rather a gradient that went from one shade of blue on the horizon to another shade of blue high in the sky. The use of colors has a big impact on our lives, whether or not we even realize it. Colors impact communication by emphasizing trends, outliers, or groups of data points. Color gradients can be especially impactful, but they do present their own set of challenges for setting them up, which we'll cover in this edition of the newsletter (see the inspiration in the article cover image).
The Latest in Power BI Weekly
One of my recent videos released in the Power BI Weekly series explores how to add conditional colors to existing visuals, including how to set up a color gradient.
Color gradients are something that came out within the last few years in Power BI and they're a game changer. Below is an example of how they emphasize the growth of the US population in the Baby Boom years after WWII (inspired by a revelation a student had a few years ago in a course I was teaching). We can see the years where the population growth rate from the previous year were higher than others because they display a sharp shade of orange on the year's bar (the reverse is true for blue on years of lower population growth).
Notice how the color gradient works in this visual. They're also pretty straightforward to set up in Power BI because it does a lot of the work for us. The technicalities for how color gradients work behind the scenes are a bit more challenging though, but we're going to explore how they work in the rest of this article.
The XYZ Color Space
The XYZ color space lets us visualize colors in a two-dimensional plane. While I'm definitely still learning a lot about this topic myself, I find this is a fascinating way to understand a perspective of how color gradients work. I added a path across the space to represent going from blue to white (in the center) to orange on the other side of the gradient. In the interest of user accessibility and accounting for the estimated one in ten people in the population who are colorblind, I'm using these three colors because they are distinguishable for those who can only see a limited range of colors.
Color Gradients in R
R is a programming language for statistics, and it also lets us create some impressive data visualizations, even with just the base plot functions. I learned through trial and error before I realized I needed to look up the actual color name in R that I wanted to use instead of guessing what the color name was. We can, of course, use hex values to define our colors, but a helpful chart like the one below colors makes it easy to set colors within R scripts.
Creating Gradients
One way we can create a color palette in R is through the colorRampPalette function that's part of the base R functions (which means we also don't need to first import a library in the code first). I used RStudio for this example, but there are other similar IDE's (integrated development environments) that you can check out as well. Here are the steps I used to create the graphic below using an R script:
But that's only three colors. What it we wanted to split the color gradient into nine colors? If we assign the outcome of the hex_values variable to use colors(9) instead of colors(3), we will see this vector display nine hex color values instead of three, which we then use in the show_col function to create a table with nine squares.
It's also worth a moment to talk about how hex values work. Hex values are shorthand notation for the color values in the RGB (red-green-blue) model, which is a color model typically used in digital applications (while CMYK on the other hand is typically for print materials). Let's take the color #00008B in the last square of the 25 hex values above. We can split this hex value into three separate two-digit numbers: 00 for R (red), 00 for G (green), and 8B for B (blue).
These might not look like numbers to many, but they're actually hexadecimal values. We then need to convert these hex values (base 16) into decimal values (base 10) between 0 and 255 for each of the three RGB model colors.
There are a number of ways we can do this, but one of my favorite approaches is using Excel because I can then store the colors in a spreadsheet in the case that I personally need them again in the future. This Excel graphic shows how to convert a decimal number with a base of 10 into a hexadecimal number with a base of 16 using the function DEC2HEX. We can convert the color values the other way using the HEX2DEC function. Notice that these hex values display two digits, even if we only need one digit, like we see for the value 0.
Putting them together as an RGB color model creates the color (0, 0, 139). We can do the same conversion process for any other hex value we come across, including the ones in this R script.
Expanding the Color Value Count
If we change the number of colors assigned to the hex_values variable vector from 9 to 25, we see how the color gradient then spreads out over a wider array of colors from the diverging base colors orange-white-blue. While this makes the gradient a bit smoother, it also makes it harder to tell the difference between the colors without the hex value labels. This makes the difference between each color slightly less perceptible to the human eye.
If we increase the number of colors to 100 or even 1600 colors, notice how the gradient smooths out so much we don't even realize there are steps in the color gradient!
Applying Gradients in Visuals
Once we create the color gradient, how do we then apply it to a visual directly in an R script? We can use color gradients in many kinds of visuals in R. Here's an example of a continuation of the R script we already developed for the color gradient we just created using the colorRampPalette function applied to a bar chart visual. Notice that the height variable just contains values between 0 and 1. This means there aren't even labels on the x-axis, but that's absolutely okay because this example instead focuses on illustrating how color gradients work when applied to values on the y-axis.
At first, even I found it confusing to set color gradients like this up in R, but the key to understanding how this code works lies in the col parameter in the barplot function. The col parameter lets us pass the colors into the barplot function in R that we would like the visual to display. Like many other things in data science (and programming in general), I find it helpful to step through the process backwards to truly understand how it works.
Coming Up!
There's a lot going on right now and I'm currently playing catchup on my newsletter. I'm excited to share that all the videos from the 13 lessons in this inaugural quarter's Power BI Weekly series are now out! Check out how to drill through pages in Power BI reports and create tabs of visuals within a single report page . I'll be publishing more content (including newsletters) on these topics in the weeks to come!
-HW
Sr Designer at Corebridge Financial
2 年This is a great post Helen! As a graphic designer color is an important part of what I do for a living. I would also add, depending on your target audience, that being aware and researching ADA color considerations is becoming more and more important. For example using a single gradated color to define various bars on a chart (similar to the orange or blue bars in your example above) is a much better way of differentiating items than using alternating vivid colors like red, blue, orange and green bars. Many color blind people can barely distinguish some of these color variations. Using gradated versions of the same color makes your pieces much more ADA friendly.
?? | #powerbi101 | BI Consultant | ?? ?? Data Visualization & Modelling expert | Ready for your Data project | Microsoft enthusiast
2 年Insightful Helen Wall looking back I could redo my past reports following correct colour application techniques/ best practices
3x Microsoft Certified Power BI Data Analyst | Business Intelligence Analyst | Business Development | Tableau * Python * R * Azure * SQL * Power BI * BigQuery
2 年Insightful. Thank you sharing Helen Wall
Storyteller | Linkedin Top Voice 2024 | Senior Data Engineer@ Globant | Linkedin Learning Instructor | 2xGCP & AWS Certified | LICAP'2022
2 年Interesting share ??