Assign Bar Chart Colors in KNIME’s Color Manager

August 30, 2021 — by Rosaria Silipo

Three Shades of Green

Assigning color to data rows in plots and tables using KNIME Analytics Platform is easy with the Color Manager node. It takes care of allotting colors to groups of data rows and the points in plots and rows in tables are then colored accordingly.

But how do we color columns in a bar-based chart? How can I obtain bars in three shades of green as in figure 1? In this short "how-to" article we show the three nodes you need to create the bar chart and then assign a color to each column.

How to assign colors to bars in a bar chart
Fig. 1. The bar chart we want: money used in projects for year 2007 (dark green), 2008 (mid green), and 2009 (light green).

The bar chart in figure 1 is obtained with a Bar Chart node on project budget data. On the x-axis - the vertical one in this case, since the bar chart is set to display horizontal bars – we have the project names and on the y-axis - again the horizontal one in this case – the money used for each project in 2007, 2008, and 2009.

The Input Data We Have

The input table has one row for each project and one column for each year, which you can see in figure 2. Each bar set (y-values) in the bar chart corresponds to the value set for a given year, that is to the value set in one column. So, if we manage to assign a custom green to each column of the input data, we should get the bars in the bar chart correspondently colored.

How to assign colors to bars in a bar chart
Fig. 2. Data table with money used from budget for each project in year 2007, 2008, 2009

The Color Map We Need

If you noticed the Bar Chart node has one additional optional input port (the white triangle port). This is the port that accepts the color mapping. A color map consists of the list of columns and assigned colors, as in figure 3.

How to assign colors to bars in a bar chart
Fig. 3. Color map for column named “used 2007”, “used 2008”, “used 2009”

The trick then is to transpose the column headers as to get just a list of column names. Such list can then pass through a Color Manager node for color assignment. The node that performs such a transpose of the column headers is the Extract Table Spec. This node indeed takes the column headers and other column information and reports them as rows into a new table, something like figure 3 without the colors.

The Nodes We Used

Thus, we need three nodes to create the bar chart in figure 1, with the three shades of green (Fig. 4). The links behind the nodes take you to the specific entry on the KNIME Hub for that node, which includes a description of what the node does, plus links to example workflows featuring that specific node.

How to assign colors to bars in a bar chart
Fig. 4. The nodes we need to color columns and bars

To assign a color to each column, we transpose the column headers (“used 2007, “used 2008”, “used 2009”) into values of a column through the “Extract Table Spec” node. This node extracts the column properties – column headers, maximum and minimum values, etc… - and puts such information, including the column header names, on data rows.

This table is then passed through the Color Manager node for color assignment to each value in the newly created column “column names”. Here, we become a bit more graphically creative, and we assign dark green to value “used 2009”, mid green to value “used 2008”, and light green to value “used 2007” (Fig. 5).

How to assign colors to bars in a bar chart
Fig. 5. Shades of green assigned to the yearly columns in the Color Manager node

This map is now sent to the second input port of the Bar Chart node to color the bars in the chart. Indeed, the second input port of the Bar Chart node, and of many other visualization nodes, accepts a map (<column header>, <column color>) to apply to the chart.

The result is the bar chart we wanted, shown in figure 1, with the three shades of green.

Download the workflow KBL:Producing Dashboards that we used for this example.

The Book We Wrote

This and more tricks have been added to the new chapter 6 of the updated edition of the ebook, “KNIME Beginner’s Luck”. “KNIME Beginner’s Luck” helps you take the first steps into basic usage of KNIME Analytics Platform. The ebook has been updated to version 4.4 of KNIME Analytics Platform and expanded with more features and procedures to build dashboards. The new chapter 6 shows you how to build simple yet dashing dashboards on your data. This post is just an extract of a small trick described in the new version of the ebook.

How to assign colors to bars in a bar chart
Fig. 6. The “KNIME Beginner’s Luck” ebook

As a bonus for having read this post till the end, use the promotion code BLOG-KBL to download a free copy of KNIME Beginner's Luck from KNIME Press 😃

You Might Also Like

How to Build Pivot Tables - A Vlog

To pivot or not to pivot, that is the question. Did you know that a pivot table allows you to quickly summarize your data based on a group, pivot, and ag...

March 4, 2019 – by Maarit Widmann &  Casiana Rîmbu

How to Keep Bias out of your AI Models

Bias in artificial intelligence (AI) is hugely controversial these days. But artificial intelligence models are empty, neutral machines. They will acquire a bias when trained with biased data. So how do you keep bias out of your model?

February 13, 2020 – by Rosaria Silipo

How to deal with missing values

In todays blog post I want to explore some different approaches to dealing with missing values in data sets in the KNIME Anlaytics Platform. Missing data is a problem that most people have to deal with at some point, and there are different approaches to doing so.

October 22, 2014 – by Aaron Hart

What are you looking for?