I am learning qgis and running into a problem. Here is what I want to do.

I have county data. It falls into 4 types. Each type includes a discrete set of counties, that does not overlap with the other types. Within each set, counties are assigned continuous values.

I have each type as a separate field in an attribute file. That is, values for type 1 are in a variable called "type 1" and includes values only for counties within that group. Remaining counties are missing values. Then type 2 counties have are in a separate field, with values for those counties, and everything remaining is missing. ditto for type 3 and 4.

How can I include all four attributes in one map?

Maybe a different way to say it is: I have four categories, and I want to show variability within each category, but maintain their categorical distinction. So counties in group 1 could be red, with lower to higher values going from lighter to darker red. counties in group 2 could be blue, with lower to higher values going from lighter to darker blue. And so on.

Does that make sense? if so, how do I do it in QGIS?


You need to use Data defined properties. In the Style tab of the layer properties, select Single Symbol (yes, I know it sounds counter-intuitive - bear with me though). Then click on the Fill Type (default is Simple Fill). Next, in the right-hand pane look for the Data defined properties button below all the controls for fill type etc. Click it. Check 'Color' in the new dialog box and then click on the curly 'E' button to create an expression.

A standard QGIS expression dialog box opens. You will now need a conditional statement. The exact statement will depend on your fields and data, but you will need something like this:

WHEN "cntry" = 1 THEN tostring("value") || ',0,0' 
WHEN "cntry" = 2 THEN '0,' || tostring("value") || ',0' 
WHEN "cntry" = 3 THEN '0,0,' || tostring("value") 
WHEN "cntry" = 4 THEN tostring("value") || ',0,' || tostring("value") 

In this case QGIS expects the expression to return a string in the form 'red,green,blue,alpha' where red is a numeric value in the range 0 to 255 and so on (e.g. '255,0,0' is red and a zero alpha value is assumed so we can leave off the fourth parameter). So, the code above tells QGIS to use set the colour relative to the value of the field "cntry" (1 = red, 2 = green, 3 = blue and 4 is pink) and the darkness of the colour relative to the field "value". The double pipe symbol is a standard QGIS concatenation by which we join the stringified value field to another string containing couple of comma separated zeros, which gives us an output in the expected format.

If you want a better spread of colours you could use a multiplier for "value". Let's say your values run from 0 to 10 then you could use tostring("value" * 25.5) or create a new field to normalize your values into 'bins' or on the scale of 0 to 255 )you may need a colour for zero too depending on your use case but we're getting into fine details where your knowledge of your data will guide you.

  • thank you this is extremely helpful. I can't quite get it though. The map is coming out entirely black. Also, I'm not sure what should go in the final "value." Is it the values for for the fourth group?
    – ameyers
    Nov 6 '14 at 14:38
  • You need to substitute your own fields for the examples I have provided. The "cntry" field is your category (which dictates the colour) and the "value" field is what dictates how bright the colour should be. If "value" only has a limited range (say 1 to 10) then you will need to normalize it to a scale of 1 to 255 to avoid everything being black. see my last paragraph. Nov 6 '14 at 15:32

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.