I have a shapefile for all US ZCTAs. Each ZCTA has been clustered by proximity and given a value in a new column called Zip_Rollup.

I am trying to add a style by value using the Zip_Rollup values. This works great except that it only allows 11 unique values and the rest are lumped as “others” with the same color making it hard to distinguish them as unique regions.

Zip_Rollup has 1000 + unique values which I would like to visualize.

It’s fine if there are duplicate colors. Is there a way to apply a random color to polygon fill by value in Carto, or other option to solve this problem?

I found a similar question with answer for Cartodb at the link below, but could not translate this to Carto myself.

Automatically coloring a large number of polygons in CartoDB


CARTO does not try to repeat colors for legends and because more than 7 or 9 colors does not make sense because they would be impossible to represent, the only solution left is to use a Others category.

Still, you can achieve what you ask easily using a bit of SQL and thematic mapping capabilities in CartoCSS.

First you need to classify your features using a criteria, since you don't want to put 1000 colors in your CartoCSS. The criteria is up to you, I will use for this example just the mathematical modulus of each row identifier cartodb_id. The simplest query would be then:

SELECT cartodb_id,
       cartodb_id % 5 as modulus       
  FROM populated_places

So using modulus I split my rows in 5 groups. Then you only have to do thematic mapping to select a color for each category. In this case:

#layer {
    marker-width: 6;
    marker-fill-opacity: 1;
    marker-allow-overlap: false;
    marker-line-width: 0;

    [modulus=0]{marker-fill: #5F4690;}
    [modulus=1]{marker-fill: #EDAD08;}
    [modulus=2]{marker-fill: #CC503E;}
    [modulus=3]{marker-fill: #0F8554;}
    [modulus=4]{marker-fill: #73AF48;}    

enter image description here

  • Thanks for the input! This is a helpful trick, but I failed to note that I am working with text, & this operation only seems capable for numbers. Is there a quick way to do the same for text, aside from assigning a numerical value? – Sbraund Jun 3 '18 at 21:15
  • Well, that's really up to you, and how your data is shaped: using the string length, grouping by prefixes, etc. etc. – Jorge Sanz Jun 4 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.