Traversing Color Gradients
PC: The Spruce (cropped a bit!)

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.

No alt text provided for this image

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).

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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:

  1. Import the scales library, which we'll use to create Step 4.
  2. We'll then create a colors variable, which is an object created by the colorRampPalette function to store the scale of the colors we pass into this function through the text values that correspond to R colors in the vector c('darkorange1', 'white', 'darkblue').
  3. We split the colors variable into actual color values by passing the number of colors we want our gradient to create, which in this case is 3. We'll then assign it to a new variable called hex_values. If we inspect the hex_values variable, we'll see it contains a vector of three hex color values because we specified it to create three (3) color values from the colors variable.
  4. The show_col function in the R scales library lets us create a table to display the hex value labels and the actual color they represent for the human eye. We see the output is a palette consisting of three discrete colors of orange, white, and blue that also match the R color names we specified in the colorRampPalette function a few lines before that. I specified the columns in an optional parameter for the show_col function as ncol = 3 because I wanted all the colors to display in a single row.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

  1. HEX2DEC("00") = 0 (red)
  2. HEX2DEC("00") = 0 (green)
  3. HEX2DEC("8B") = 139 (blue)

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

No alt text provided for this image

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!

No alt text provided for this image

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.

No alt text provided for this image

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.

  1. Notice the values in the height variable are in decimal values between 0 and 1. We first need to scale this up so that they display as whole numbers, which in this example we can do by multiplying each value in the height vector by 100 to get values of 2, 10, 15, 22, 33, 50, 55, 61, 89, and 99.
  2. We need to use these scaled values for the height variable in Step 1 to extract a hex color value from the hex_values vector variable. R uses 1 as the base for the index instead of 0 (like we see in languages like Python), so the height value 0.89 translates to hex_value[0.89*100], which equals hex_value[89] which then returns the hex color value of #3838A4 .
  3. The code snippet hex_values[height*100] gives us a vector consisting of the hex color values #FF8105 , #FF962E , #FFA348 , #FFB56C , #FFD1A4 , #FFFDFC , #E7E7F4 , #C8C8E6 , #3838A4 , and #05058D .
  4. Lastly, the barplot function creates the bar plots with the height vector we pass into it, which we then specify the colors for each height value through the col parameter within the function using the hex values determined by the colors in the hex_values variable in Step 3.

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

Robbin Colgrove

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.

Juan-Pierre Louw

?? | #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

Kelvin Okpala

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

POOJA JAIN

Storyteller | Linkedin Top Voice 2024 | Senior Data Engineer@ Globant | Linkedin Learning Instructor | 2xGCP & AWS Certified | LICAP'2022

2 年

Interesting share ??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了