3

In the legend of the print composer I have a layer with attributes that have the same symbology (colour) (see screenshot). What I want is to have headlines in the legend to describe the attributes with the same colours. For excample "Hedges and bushes" oder "Forests",.. They should be placed above, as marked with arrows. How can I get that?

enter image description here

2 Answers 2

2

As far as I'm aware, there isn't a neat way to add 'sub-groups' with headings within the legend for layers that have categorised symbology like this. Note - you can group multiple layers and give them sub-headings, but you cannot group multiple symbols within a single layer's style definition.

What you could do as a workaround is add the layer to your legend n times (once per category) and remove the entries which don't belong in that category. You could then rename each layer instance to reflect the relevant category name:

enter image description here

However, I would take a different approach instead. As the symbology is exactly the same, I would be inclined to merge the symbols for all of the unique vegetation types within each category, and label by category (e.g., Forest). You can do this in layer properties

screenshot from layer properties merging symbols

Then, your symbol for "Forest" will only appear once in your legend, rather than being duplicated across all of the forest types.

It seems like your labelling is what differentiates the individual types within vegetation categories (e.g., Forests), so optionally, you could also include a label legend. You can enable label legend in Layer Properties -> Legend. Which will give you a label symbol in your legend within the print layout explaining what the label text means.

Label legend settings

If you really need the legend to look how you have described (i.e., a sub-heading for "Forest" with n number of duplicate symbols with different labels), and you don't like the clunky workaround at the top of my answer, you could generate a layer per category using Split Vector Layer.

split vector layer tool

This depends on having a field which denotes the category in your attribute table. It will return 1 layer per category, so you would have them as individual entries in your attribute table, and you could give them headings as required.

You could package these layers (one per category) into a single Geopackage, so your dataset would still be on the disk as one file, but each vegetation class would be a separate layer and you'd have more flexibility in setting out your legend.

1
  • 4
    Instead of generating one layer per category using Split Vector Layer, a virtual layer per category would be an option. Advantage: it will automatically be updated when changed. Commented Aug 14 at 13:14
0

Inspired by your suggestions, I found the following solution: I created duplicates of the original layer for each vegetation category, which now represent my headings in the legend. I merged the vegetation types with the same symbol so that identical symbols are not repeated in the legend. However, the vegetation types are all on one line in the legend, so I have to manually break the line after each vegetation text. Whenever I update the legend, this is undone and I have to break the lines again, which is tedious. Is there a simple solution for this?

enter image description here

3
  • 2
    When you click "Update All", QGIS pulls through the "Legend" label from the Layer Properties. By default when you merge, that will be a ';' separated list of all the classes you merged. This is what you're manually breaking in the Legend object in the print layout. If you wrap your current label with this expression syntax, it will replace each semi-colon with a line-break and feed through when you update the legend: [%replace('Habitat 1 (code); Habitat 2 (code)',';','\n')%] You could also manually swap each ';' for a '\n' and save the string in a [% 'string' %] wrapper. Commented Aug 15 at 11:51
  • Great, that works well! Can you give an excample of "saving the string in a wrapper"? I think it could be helpful, but don't get it right.. Commented Aug 15 at 20:55
  • 2
    Sure, what I mean is that anything you write wrapped in [% and %] will be treated by the legend as an expression, so you can include line breaks as "\n". You could write [%"Line 1\nLine 2\nLine 3\n%] and the legend would have the text split over three lines. Commented Aug 18 at 15:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.