Using categorized classification method for creating a legend on QGIS permits to create an automatic legend based on the values or categories defined in on item or a combination of items, or a formula that define the categories. The legend is ordered in ascendent or descendent form using the values detected on that item or formula. But are there a way where the legend can be ordered using an additional item that works like an index?

Let give me an example: in geology, names like Quaternary, Paleocen, Eocene, etc. are referred to diferent geological ages and cannot be ordered in a legend by alphabetic order. They must be ordered from younger to oldest ages. In this case it is necesarry to use a different item that acts a index and can permits the appropiate display of the legend.

  • You can manually rearrange the order in which the categories are displayed. – csk Jul 1 '19 at 19:06

The order that categories are displayed in a categorized symbol cannot be directly and easily sorted by a separate "index item". Here are a couple of workarounds that I think will each do part of what you want to achieve.

Method one: manually re-arrange the items in the legend.

In the layer styling panel, drag and drop the legend items into the desired order. If you have many tens or hundreds of categories, this method will take a long time. Use the next method instead.

Method two: concatenate the index field with the category field.

Add an "index" field to the attribute table, where the index values represent the sort order for the categories. Using the geologic ages in the order you provided them (Quaternary = 1, Paleocene = 2, Eocene = 3), use the field calculator to assign the correct index value to every feature with this expression:

case when "age" = 'Quaternary' then 1
 when "age" = 'Paleocene' then 2
 when "age" = 'Eocene' then 3
 else 0

Set up a categorized style as usual, but instead of selecting the "age" field from the Column menu, enter this expression:


The categories will be

  • 1_Quaternary
  • 2_Paleocene
  • 3_Eocene

Because the index value comes first, the categories are automatically sorted by the index value.

enter image description here

If you want the legend to show only the geologic age without the index number, edit the legend value.

enter image description here

enter image description here

Note that you don't have to use an underscore in the concatenate expression. For manually editing the display values, it might be more convenient if the index and age were separated by a space instead of an underscore, like this: concat("index",' ',"age"), or without any character separating them, like this: concat("index","age")

  • Yes, at present seems that are the only possible methods for ordering class as needed. Thanks for your advise – Gabriel Asato Jul 1 '19 at 20:47
  • At last what I did was to add an index number to each age as you explained and I edited using vi the resulting qml because editing legend label by this way is much faster tha using the QGIS GUI. – Gabriel Asato Jul 2 '19 at 22:15
  • @GabrielAsato Can you post an answer where you explain the process for editing the qml? I think that would be useful for others who have the same question. – csk Jul 3 '19 at 15:28
  • Procedure for editing the qml is below – Gabriel Asato Jul 5 '19 at 18:18

After procedure where "index" (in this case "age") and the categorical item ("name) were joined we need to edit each label one by one because the legend doesn't looks like we want . This is a very hard task but it can be facilitated by saving the symbology as qml and edit it by using a common text editor.

qml are schemas written using XML, then is easy to identify the parts, categories, leaft, etc.

In this case the tag that identify each category is named <category>. Inside it is possible to distinguish two attributes, value and label. "Value" is using for creating the categories and "label" is only that, a text that describe the category. For that reason "label" can be modified despite what are in "value". In the other way you cannot modify "value" without a consecuence at least you know what are you doing.

Let see and example:

<category label="Agua de Castilla" value="312200.Agua de Castilla" symbol="4" render="true"/>
<category label="332100.Agua caliente" value="332100.Agua caliente" symbol="5" render="true"/>
<category label="332100.Brecha Centenario" value="332100.Brecha Centenario" symbol="6" render="true"/>

Look at the first row, "label" was edited and the number that correspond to the index was deleted. This kind of procedure will no affect the normal behaviour of the symbology.

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.