7

I am a newbie working on QGIS 3.6 / Windows 10

I need to style a legend for map with headings and sub headings The main divisions of the legend item need to be subdivided into sub-classes

i.e. The topmost items each represent a group of biotope types represented as the categorised colours of the polygon features in the map. - see herefor details: https://www.stadtentwicklung.berlin.de/umwelt/umweltatlas/ed508_04.htm

the "sub-classes" of each of these group of biotopes have individual biotope-codes that I need to label as shown in the other question I posted here: rule based coloured background for labeling in QGIS

I have attached a pic of the legend as it should look below:

enter image description here

1

2 Answers 2

7

The way to get QGIS to put sub-categories into a print legend is to make the sub-categories part of the layer's style. This means that converting your rule-based labels (with colored backgrounds) into part of the symbol style. If this method seems like too much work, see below for a rather hack-y workaround.

  1. Convert your categorized style to a rule-based style (change it in the dropdown menu in the style panel). The categories will automatically be converted into rules.

    enter image description here

  2. Add a new rule and nest it under the corresponding category.

    • Use the same filter for that rule as you used for the rule-based label.
    • To add the label text, use a centroid style for the rule with a font marker style. Click the data-defined override next to the character selector, and choose the label field.

      enter image description here

    • To add the colored label background, add a symbol layer to the centroid style, choose "simple marker" for the symbol type, and use the rectangle with a 90 degree rotation. Adjust the marker offset and size so that it surrounds the label text. If desired, use an expression based on the length of the label string to control the size of the rectangle.

      enter image description here

    Note: The "rectangle" symbol has fixed proportions, which means if you make it wider to accommodate a longer text string, it also gets taller. If this is an problem, you might want to look into using a geometry generator symbol layer, although those are more complicated to set up. But your labels appear to be all about the same length, so you can probably use the same size rectangle for all of them.

  3. Make a copy of the rule you created in step 2. (Right click on a rule to make a copy.) Edit the copy to make the next rule. Repeat until you have a rule for every sub-category.

    enter image description here

  4. Add a legend to your print layout.

    enter image description here

    Adjust the legend item spacing as needed to make everything fit.

    enter image description here


Since you already went to the effort of making rule-based labels, the method above might seem like too much work. In that case, you could leave the rule-based labels as they are, and instead create the legend manually. Here's the basic method:

  1. Turn off auto-update on the legend item. Select and remove all the legend items and categories except for the first category (biotope type).

    enter image description here

  2. Repeat step 1 for each biotope type. You should now have a separate legend for every category in your categorized style (biotope types).

    enter image description here

  3. Add print layout Label items to re-create the label text and background styles. Note that each sub-category requires two separate label items: one with the label text and background color, the other with the sub-category description.

    enter image description here

2
  • thx, I got a little stuck in point 2. finding the "font marker style" I found it by using the drop down menu at 'Symbol layer Style' to change it from the default "simple markers" to "font markers" Commented Aug 23, 2019 at 17:18
  • @csk I have a follow up question regarding the length of the rectangle geometry generator posted as a separate question. Could you help pls.pls.pls?gis.stackexchange.com/questions/340937/… Commented Nov 6, 2019 at 14:02
3

You can set text to be on the legend if you go to Layer Properties > Legend > Text on Symbols and either manually type in the text you want for each symbol, or use 'Set Labels from Expression' and enter the field name that will appear in your label (this works once-off, it is not dynamic). See below.

enter image description here

Unfortunately the label style will be the same for each symbol; the background for those labels cannot be data-defined even though it looks possible (i.e. if you have a column storing the label background colour, it cannot access that to have a different background colour for each symbol). So you cannot use that to show what the label would really look like.

One approach could be to have a layer where your data looks the way it's meant to be on the canvas, and a duplicate of that layer that isn't visible on the canvas with symbology set up purely for legend purposes.

Here is an example - test shows the polygons styled with colours and text labels the way they're meant to be. test_labels is a duplicate of the same data, but with a symbology that would look wrong on the canvas but is handy to use in the legend.

enter image description here

And here is what it looks like in a print layout - the map shows test but the legend is of test_labels (with the text added in the legend as seen in the first screenshot in this post).

enter image description here

To make the polygon symbols in the legend look longer than the default, go to the Legend Item properties > Symbol > Symbol width and height

enter image description here

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.