4

I am a newbie working on QGIS 3.6 / Windows 10. I need to label a polygon layer which is categorised by colour. (pls refer to attached pic as a graphical reference)

enter image description here

The labels need to have a background which is also categorised by colour (red, orange, yellow and white) according to values in the attribute table of the shapefile.

My understanding is that the map as pictured has been created with an older version of ArcView...

Is it even possible to achieve something similar in QGIS?

1
  • The ability to use the 'single label' method seems to not be available for users with QGIS v3.22.
    – Hims18
    Sep 13, 2023 at 10:16

6 Answers 6

6

The easiest way is to simply use the @symbol_color variable in the label background expression.

This ensures whatever color your symbols are, your labels are automatically updated, without having to set a seperate CASE statement for the labels:

enter image description here enter image description here enter image description here

2

In the attribute table of your polygon, you will have a field which dictates the polygon colour, another which dictates the label colour, and another which dictates the label text. I've made a quick example below.

enter image description here

You can set the layer symbology to 'Categorized' based on your polygon colour field.

enter image description here

And set you labels to 'Rule-based labelling'. You'll need to set rules as required for your dataset, my example rules follow the format "Label colour" = 'yellow' etc. enter image description here Example output below.

enter image description here

2

Yes, this is possible. In the Layer Properties you can set you labels. You can either set rule based labeling with a filter expression for all your categories with a defined background. Or you can add single labels and add a expression similar to this for the background color:

CASE 
  WHEN   "objectid" = 1 THEN color_rgb( 0,0,0) 
  WHEN   "objectid" = 2 THEN color_rgb( 10,0,0)
 END

You can find more on labeling in qgis here: https://docs.qgis.org/testing/en/docs/training_manual/vector_classification/label_tool.html

2

Yes it is possible. In the labeling tool, enable Background

enter image description here

and in the Data defined override write the following

Case
When "id" in (1,5,9,15) then 'yellow'
When "id" in (2,7,10,13) then 'blue'
When "id" in (4,11,14,119) then 'orange'
else 'white'
end

Change the "id" to the field name that contains the numbers for your case, and the change numbers in the brackets to the numbers that you want to highlight.

Here is the output:

enter image description here

1

You're able to make your label's background "data-defined" :

In your layer's properties go to label/background/stroke color click on the drop-down menu and make the colour data-defined using the expression builder. You able to reference any of your table's attribute and make it conditionnal (if needed) using if or case statements.

1
  • stroke or fill color ... to be more precise ...
    – Snaileater
    Aug 22, 2019 at 8:56
0

Yes, it's possible. Here's an example where the background color of the label (the name of the flower) depends on the specie (field "art").

enter image description here

Go to the layer's styling, choose which field to label (here "art") and background. (Alt. choose Layer Properties - Labels - Background)

enter image description here

Set rules for the fill color: click on the button to the right enter image description here and choose Edit. (After a rule is set, this button turns yellow.) Use conditionals case when "art"= ... (the field "art" is set to be the label) and color_rgb to set the color (or color_cmyk a.o.). Use color_rgba or color_cmyka if you want to add alpha (transparency) component.

enter image description here

Looking for colors: http://colorbrewer2.org - gives you rgb and cmyk values.

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.