4

I am a total beginner to QGIS and still struggling with the basics. After a quick research on this forum I could not find the answer to my current issue.

What I am trying to do is to colour the regions of a country shapefile where using the information stored in a series of categorical variables. So for each variable if that category is "yes" I want the region to be coloured in one colour (also I would later like to have a legend for the colours, but I'll worry about that later..). I have started doing this using rules-based styling in the attribute table, but I am unsure how to do it when there is conflicting rules, when a region has multiple entries of "yes". If I use single rules, then colours are overwritten by the rule hierarchy. Ideally I would like to have this region colours in stripes.

Does anybody have an idea how to do this?

Here is a description of my data structure.

enter image description here

1

3 Answers 3

1

If you have only a few categories, you can create a separate rule for each possible combination of yes's and no's, for example if you have only two categories, your set of rules would look like this:

"cat1" = 'yes' AND "cat2" = 'yes'
"cat1" = 'yes' AND "cat2" = 'no'
"cat1" = 'no' AND "cat2" = 'yes'
"cat1" = 'no' AND "cat2" = 'no'

The number of possible combinations is the square of the number of categories (3 categories = 9 combos; 4 categories = 16 combos; 5 categories = 25 combos; etc.). This quickly becomes an unmanageable number of combinations. So a more automated solution would be best with more than 4 categories.

Let's set this up with nested rules. The larger rules will group categories by how many different 'yes' categories they have. A true/false statement like ("cat1" = 'yes') will evaluate to 1 when true, 0 when false. So a sum like this:

("cat1" = 'yes') + ("cat2" = 'yes') + ("cat3" = 'yes') + ("cat4" = 'yes')

will give a count of how many of the categories say 'yes'. Create 5 rules without symbols, like so:

("cat1" = 'yes') + ("cat2" = 'yes') + ("cat3" = 'yes') + ("cat4" = 'yes') = 1
("cat1" = 'yes') + ("cat2" = 'yes') + ("cat3" = 'yes') + ("cat4" = 'yes') = 2
("cat1" = 'yes') + ("cat2" = 'yes') + ("cat3" = 'yes') + ("cat4" = 'yes') = 3
("cat1" = 'yes') + ("cat2" = 'yes') + ("cat3" = 'yes') + ("cat4" = 'yes') = 4
ELSE

Nest 4 rules under the first rule, with simple fill style: "cat1" = 'yes' "cat2" = 'yes' "cat3" = 'yes' "cat4" = 'yes'

Choose the colors you want. Save the colors as swatches to make them easy to access in the following steps.

Right-click on the second rule > Refine Current Rule > Add Categories to rule. Create a style with two stripes.

  • Line pattern fill, set line width to 1/2 the value you choose for line spacing.
  • Add a second layer to the simple line (under line pattern fill). Use the same line width as the first layer; use the same number for "offset".

enter image description here

Add 5 styles, then return to the main style panel screen. Change the rules as follows:

("cat1" = 'yes') and ("cat2" = 'yes')
("cat1" = 'yes') and ("cat3" = 'yes')
("cat1" = 'yes') and ("cat4" = 'yes')
("cat2" = 'yes') and ("cat3" = 'yes')
("cat3" = 'yes') and ("cat4" = 'yes')

Change the stripe colors to match the colors you used for the single-category group. Use color swatches to get exactly the same color.

Repeat for the 3-category and 4-category groups.

4
  • Thanks csk, this sounds like what i wanted to do. I will try to implement it.
    – Papayapap
    Commented Nov 22, 2018 at 5:22
  • Actually, I think I will need to do the unautomated solution, because, if I get it correct, because I don't only need the count of yes.
    – Papayapap
    Commented Nov 26, 2018 at 7:32
  • Somehow, when I do this approach with many lines, they always overlap. Did you also get this problem?
    – Papayapap
    Commented Nov 29, 2018 at 9:23
  • I didn't try it with more than two lines, but that doesn't surprise me. I've had a similar issue with other symbol types, where the offset doesn't give a consistent appearance.
    – csk
    Commented Nov 29, 2018 at 17:37
0

If you have in the attribute table YES and other strings as shown in the image below, you can do the following:

enter image description here

  • Go to Rule-based Style
  • Add this Rule in the Filter: "FieldName" = 'YES' #You need to change FieldName based on what you have in the attribute table. In this Example: "YES_NO" = 'YES'

enter image description here

  • Add a new rule and in the Filter section select ELSE

enter image description here

  • The Rule based Style should look like this:

enter image description here

Here is the output:

enter image description here

6
  • Thanks ahmadhanb for the very clear illustration. However, for me I know how to do the single rule colouring. What I am struggling with is how to do it with multiple rules. In my data I have 6 yes_no variables and I need to colour the map with different colours for each indicator. So, I will have overlaps where more than one variable has the answer "yes" and then there will be conflicting colours. Do you know how I can do it such that in this case the field will be striped with both colours?
    – Papayapap
    Commented Nov 19, 2018 at 13:12
  • @Felix Could you please add a screenshot of part of your attribute table so that your idea can be clear to the community?
    – ahmadhanb
    Commented Nov 19, 2018 at 13:37
  • You need to look at the conditional expression. A case statement should be able to do what you are looking for Commented Nov 19, 2018 at 16:38
  • Dear ahmadhanb, I have added a fictional screenshot of my data in my question. Thanks @kartoza-geek for the hint on conditional expressions. Could you perhaps expand on it?
    – Papayapap
    Commented Nov 22, 2018 at 5:19
  • @Felix You are making things complicated, you can simplify use categorize symbology and classify by region field. Then use a table whether in the layout or the manuscript to define what each region represents.
    – ahmadhanb
    Commented Nov 22, 2018 at 7:59
0

You can use a nested rule based symbology like this:

enter image description here

The hierarchy order starts at the bottom. Means: if one polygon has 1 = yes and 2 = yes it will use the style of 2. Using transparent styles (like 3 and 5 within 1; the polygon in the middle in my example) you can also use combinations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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