# Style qualitative data using hex color codes

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty banner" width="175"><figcaption></figcaption></figure></div>

## **Context**

In this guide, we're going to uncover how to use hex color codes in Builder to bring qualitative crime data from Chicago to life. Ever wondered how to give each crime category its own unique color? We'll show you how to do that with randomized hex color codes. We'll also dive into setting specific colors based on conditions, tapping into the power of CARTO Workflows and SQL. Once we have our colors ready, we'll use Builder's HexColor feature to effortlessly style our layers. By the end of our journey, you'll be ready to create a vibrant and clear map showcasing the intricacies of crime in Chicago. Excited to transform your data visualization? Let's jump right in!

## **Step-by-Step Guide:**

In this guide, we'll walk you through:

* [Creating hex color codes with Workflows](#creating-hex-color-codes-with-workflows)
* [Generating hex color codes via SQL](#generating-hex-color-codes-using-sql)
* [Styling layers in Builder using hex color codes](#styling-qualitative-data-using-hex-color-codes-in-builder)

### **Creating hex color codes with Workflows**

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty banner" width="175"><figcaption></figcaption></figure></div>

1. Access *Workflows* from your CARTO Workspace using the Navigation menu.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FF1QyJgCH7cU0HYTYkzyK%2Fimage.png?alt=media&#x26;token=317527bb-d374-4cc6-88d0-856e997393b8" alt=""><figcaption></figcaption></figure>

2. Select the data warehouse where you have the data accessible. We'll be using the CARTO Data Warehouse, which should be available to all users.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FvwPYLN8rDkN8EeEBWD81%2Fimage.png?alt=media&#x26;token=f72072d0-979c-4aeb-84bd-a306feea47c6" alt=""><figcaption></figcaption></figure>

3. Navigate the data sources panel to locate your table, and drag it onto the canvas. In this example we will be using the `chicago_crime_sample` table available in demo data. You should be able to preview the data both in tabular and map format.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FtlePVri03tBrtWi1MYki%2Fimage.png?alt=media&#x26;token=a48ab890-f1dc-4ce5-917f-6090421fffaa" alt=""><figcaption></figcaption></figure>

4. We are going to generate random hex color codes based on distinct category values. For that, add the **Hex color generator** component into the canvas. This component will first select the distinct values of an input column and then generate a unique hex color code for each of them. In our case, we'll select `primary_type` as the column input, which defines the type of crime committed. Then, we click on "Run".

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FXdlFF0gk4h7cuMo9Khnk%2Fimage.png?alt=media&#x26;token=77138da0-fe27-495a-bdbc-a860556913f6" alt=""><figcaption></figcaption></figure>

5. We can preview the data to confirm a new column named `primary_type_hex` has been added to your output table. This one contains distinct hex color values for each distinct input category.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8rwRElFhe8RERFUTvQw9%2Fimage.png?alt=media&#x26;token=680fe379-eee5-45f4-9bbe-dff817c6b6ef" alt=""><figcaption></figcaption></figure>

6. Now let save our output as a table using **Save as Table** component. We will use this output later to generate our Builder map.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FFDaPza5Ul4IBp8EECHyJ%2Fimage.png?alt=media&#x26;token=84c3d5fc-a785-418b-ad59-98e1c2060483" alt=""><figcaption></figcaption></figure>

7. Add [annotations](https://docs.carto.com/carto-user-manual/workflows/workflow-canvas#annotations) so you can provide further context to other users accessing the Workflows.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2Ft6WYQftTAZs8YToDE3RR%2Fimage.png?alt=media&#x26;token=2217d1c3-4538-4213-958b-a9a85934f21c" alt=""><figcaption></figcaption></figure>

Now that we're done with the first step of having a table ready to visualize the specific locations of crimes, we'll move to the generation of a new separate table for an extra layer in the visualization. In this case, we'll leverage [Spatial Indexes](https://academy.carto.com/building-interactive-maps/data-visualization/broken-reference) to gain insight into the ratio of arrested to non-arrested crimes. By doing so, we can better grasp the geographical distribution and patterns of resolved versus unresolved crimes.

8. First, transform the crime point location to H3. To do so, use the **H3 from Geopoint** component using `12` as the resolution level. Once run successfully you can preview the data and the map results.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FbWan0MkPGzA4htuSkyjd%2Fimage.png?alt=media&#x26;token=bfb1e9d2-44dd-4070-a8e6-29b95af8f392" alt=""><figcaption></figcaption></figure>

9. To discern if a crime resulted in an arrest, we need to convert the `arrest` column from a `Boolean` type to a `String` type. We'll accomplish this transformation using the **CAST** component.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FRMH0BCbWkvafM7nH22HF%2Fimage.png?alt=media&#x26;token=bfc983cf-dfcb-4e70-b332-114e56fd12d2" alt=""><figcaption></figcaption></figure>

10. Now we can use **Simple Filter** component to identify the crimes that were *arrested* (True) vs *not arrested* (False).&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FSqc8ruJAVWQf42t7shmA%2Fimage.png?alt=media&#x26;token=12ffe60a-b68f-4ddd-a2b5-bd109a0fd4cd" alt=""><figcaption></figcaption></figure>

11. For each **Simple Filter** output, we will add a **Create column** component where we will define a specific hex color code value to the same column named `arrest_hex` as per below screenshot. Let's also add some annotations so is clear what we are doing on these steps.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FbW9nR1XhTsU10I3gBGZ7%2Fimage.png?alt=media&#x26;token=638b6bfa-1206-4f66-a07f-e84ebd415c52" alt=""><figcaption></figcaption></figure>

12. Now that we have generated the `arrest_hex` column, we will use **UNION** component to bring our dataset back together.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8xQdlYodcG4dPXIrypad%2Fimage.png?alt=media&#x26;token=076822f1-5cc1-4065-bad9-96d0047f6097" alt=""><figcaption></figcaption></figure>

13. Finally, let's save our results in a new table using **Save as Table** component.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FnQF6Ro4Uo1jb94RzYIeN%2Fimage.png?alt=media&#x26;token=a6c7a83d-7b0a-4ca2-9a95-6dba37e60eaa" alt=""><figcaption></figcaption></figure>

***

### **Generating hex color codes using SQL**

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FhB2W9xXbzzo0kEuXMe3S%2Fintermediate%20banner.png?alt=media&#x26;token=4acd2cc7-c7e8-46c0-9669-6f6b73c030dd" alt="Intermediate difficulty banner" width="175"><figcaption></figcaption></figure></div>

We can generate Hex color codes directly using SQL, both in the DW Console and the CARTO Platform. Within CARTO, you have the flexibility to use either the Workflows with the Custom SQL Query component or the Builder SQL Editor.&#x20;

Below you can find two different examples on how you can use SQL to generate hex color codes:

* Define hex color codes using CASE WHEN statement:

```sql
WITH data_ AS (
  SELECT 
	geom,
    CAST(arrest as string) as arrest
FROM carto-demo-data.demo_tables.chicago_crime_sample)

SELECT
	a.*,
	CASE 
        WHEN arrest = 'true' THEN '#8cbcac'
        WHEN arrest = 'false' THEN '#ec9c9d'
        ELSE ''
    END AS arrest_hex
FROM data_ a
```

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F82UUJzMfiaI7sCuzNRYX%2Fimage.png?alt=media&#x26;token=4cf1ce67-865f-4b2e-b23b-ee7f39b954b5" alt=""><figcaption></figcaption></figure>

* Generate random hex color code values for each DISTINCT category value:

```sql
WITH data AS (
  SELECT DISTINCT primary_type
  FROM carto-demo-data.demo_tables.chicago_crime_sample
),
hex_ AS (
SELECT 
  primary_type,
  CONCAT(
    '#',
    LPAD(FORMAT('%02x', CAST(RAND() * 255 AS INT64)), 2, '0'), -- Red
    LPAD(FORMAT('%02x', CAST(RAND() * 255 AS INT64)), 2, '0'), -- Green
    LPAD(FORMAT('%02x', CAST(RAND() * 255 AS INT64)), 2, '0')  -- Blue
  ) AS random_hex_color
FROM data)
SELECT 
    a.geom, 
    a.unique_key, 
    a.primary_type, 
    b.random_hex_color
FROM carto-demo-data.demo_tables.chicago_crime_sample a LEFT JOIN hex_ b 
ON a.primary_type = b.primary_type
```

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FxGmPYz63urJfgSYtW3BC%2Fimage.png?alt=media&#x26;token=efca5838-bc9b-4a1b-9cde-3f94b0681fd9" alt=""><figcaption></figcaption></figure>

***

### **Styling qualitative data using hex color codes in Builder**

<div align="left"><figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8iAlvj1s2Th85m6IvcG5%2Fbeginner%20banner.png?alt=media&#x26;token=10e8f40a-3632-4920-87c3-6b0dca2a6775" alt="Beginner difficulty banner" width="175"><figcaption></figcaption></figure></div>

Now that we have generated our tables in Workflows containing the hex color code values, we are ready to style it in Builder using the *HexColor* functionality that allows you to style qualitative data leveraging your stored hex color code values.

14. First, let's load our first output table in *Step 6* named `chicago_crime_hex`. We will do so adding it as a SQL Query source. To do so, copy the qualified table name from the **Save as Table** component in Workflows or access the table in Data Explorer.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FVMjUrswA1nAUsrSgZPvy%2Fimage.png?alt=media&#x26;token=0da331c9-17f2-47ae-9449-d365c3a35db7" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F7fEmefySol8IA9puvnio%2Fimage.png?alt=media&#x26;token=7997184f-6f91-4538-8390-99856bf3017c" alt=""><figcaption></figcaption></figure>

15. Now let's rename your map to "Crime Analysis in Chicago" and the layer to "Crimes".&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FXNDAJDoboyZiLbclOd71%2Fimage.png?alt=media&#x26;token=47b50623-3581-4bb9-b152-217231c24b3a" alt=""><figcaption></figcaption></figure>

16. Now open the Layer style configuration, and follow the steps below:
    * In the *Color based on* selector pick `primary_type` column to associate with the hex color code.
    * In the *Palette* section, click on 'HexColor' type.
    * Finally, pick the column with the hex color code values, which in our instance is named `primary_type_hex`.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FvkImXmDaYCzcuIuONY36%2Fhex_tutorial_1.gif?alt=media&#x26;token=8ff108cd-c858-449a-b8da-ef826d3b7639" alt=""><figcaption></figcaption></figure>

You should now observe the crime point locations styled based on the hex color codes from your data source. Furthermore, consult the legend to understand the association between categories and colors.

17. Change the *Stroke Color* to `black` and set the *Radius Size* to `6`.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FWz15aOGq6PJnIwwaNgVV%2Fimage.png?alt=media&#x26;token=21409c12-db9a-448a-aa4e-540ec857cfcf" alt=""><figcaption></figcaption></figure>

Next, integrate the aggregated H3 grid to assess the *arrest* vs. *non-arrest* ratio. This will help pinpoint areas where crimes often go without subsequent arrests, enabling us to bolster security measures in those regions

18. For that, add a new source adding `chicago_crime_h3_hex` table created in *Step 13*. A new layer named "Layer 2" will be added to your map in the top position.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FKFSrtF8SfwCyQRre8vHr%2Fimage.png?alt=media&#x26;token=0d6ad701-584a-4c94-8a2e-5f079446d113" alt=""><figcaption></figcaption></figure>

19. Rename the new layer to "Crimes H3" and move it to the second layer position, just below Crimes point layer.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FNeBxuEaU0KEXOX65ahaH%2Fimage.png?alt=media&#x26;token=21e0fbf9-ebeb-4ace-abbb-888bb9df6572" alt=""><figcaption></figcaption></figure>

20. Next step would be to style the "Crime H3" layer. Open the *Layer style* configuration. In the *Basic* section, set the *Resolution* to `3`. This will decrease the granularity of the aggregation so we are able to visualize it with the crime point location overlaying on top.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F8E159fTxQcaSEvx8K8sB%2Fimage.png?alt=media&#x26;token=2aa57f1b-74a5-4063-bec2-13fce295c25d" alt="" width="303"><figcaption></figcaption></figure>

21. Now, let's style the cell using our stored hex color codes. For that, select `arrest` column in the *Color based on* section as the category using `MODE` as the aggregation method. Then, choose 'HexColor' type and select `arrest_hex` as your *Color Field*.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FfAiwS2QoQPh7nVjS1UlF%2Ftutorial_2.gif?alt=media&#x26;token=a93b4970-87d9-4aa1-a66d-ac2ab159e043" alt=""><figcaption></figcaption></figure>

22. To finalise with the layer options, we will set the *Visibility by zoom level* of the "Crimes" point location layer `from 11 to 21`, so that only the H3 layer is visible at lower zoom levels.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FbYPDEqXov4cKfruDfudp%2Fimage.png?alt=media&#x26;token=f3a9b95e-0d13-4b31-9b71-5335a1798d49" alt=""><figcaption></figcaption></figure>

23. &#x20;Once we have the styling ready, we will proceed to add some Widgets to our map. First, we will include a [Formula Widget](https://academy.carto.com/building-interactive-maps/data-visualization/broken-reference) with the `COUNT` of "Crimes" point source.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FEaR6fNtNEexRHwu81yUt%2Fimage.png?alt=media&#x26;token=45fdf2b1-c1a8-428d-a81c-2b1ed42ff005" alt=""><figcaption></figcaption></figure>

24. To continue providing insights derived from our sources, we will add a [Pie Widget](https://academy.carto.com/building-interactive-maps/data-visualization/broken-reference) linked to "Crimes H3" source displaying the percentage of arrest vs not arrest proportion of crimes.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2F59H4Jb8aCxDXhMqIXtoB%2Fimage.png?alt=media&#x26;token=c5924214-c5dd-4131-9b5a-f9ba757fde31" alt=""><figcaption></figcaption></figure>

25. Finally, we will add a [Category Widget](https://academy.carto.com/building-interactive-maps/data-visualization/broken-reference) linked to "Crimes" point source displaying the crimes by type as below.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FDwEtxbGnqzZ6rHxRwZoK%2Fimage.png?alt=media&#x26;token=5b004584-eb45-4a3c-b0d0-985ee3ec1f75" alt=""><figcaption></figcaption></figure>

26. Once we have finished adding widgets, we can proceed to add a [map description](https://academy.carto.com/building-interactive-maps/data-visualization/broken-reference) using markdown syntax. In our case, we will add some explanation about how to style qualitative data using *HexColor* functionality. However, you can add your description as desired, for example to inform viewer users how to navigate on this map and obtain insights.&#x20;

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FLgryMSYxoW498fwgJFd7%2Ftutorial_3.gif?alt=media&#x26;token=90f00f48-c005-4cd6-a01f-ea74bc3efe4d" alt=""><figcaption></figcaption></figure>

27. We are ready to publish and share our map. To do so, click on the [Share](https://academy.carto.com/building-interactive-maps/data-visualization/broken-reference) button located at the top right corner and set the permission to Public. Copy the URL link to seamlessly share this interactive web map app with others.

<figure><img src="https://3015558743-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFEElAdsRIl9DzfMhbRlB%2Fuploads%2FyWdvNBbewOvEK3V85Yhk%2Fimage.png?alt=media&#x26;token=16745833-cf19-43d5-8c11-1101c6e1e46b" alt=""><figcaption></figcaption></figure>

&#x20;And we're ready to visualized our results! Your map should look similar to the below.

{% embed url="<https://clausa.app.carto.com/map/a0fb6502-2c73-47d8-b4a7-0e2ecd871008>" %}
