I would like to make a legend to a map in qGIS 3.18 using proportional symbology based on an integer field. When I go to Layer Properties -> Symbology -> Advanced -> Data-defined Size Legend, I get an error message saying "Data-defined size is not enabled!". Would anyone have any ideas what could be the reason?
QGIS Error – Fix ‘Data-defined Size is Not Enabled’ When Creating Legend
errorlegendqgis
Related Solutions
If you're wanting to do this via expressions, here's a possible approach:
Under Settings -> Style Manager -> Color Ramp tab, create a new gradient ramp for your desired colors. (Hit the plus button down the bottom to add a new gradient). Give this gradient a descriptive name, eg 'blue ramp'.
You can take advantage of the "ramp_color" function in an expression. This function returns a color interpolated from a color ramp. So, if you wanted the color 3/4 of the way along the 'blue ramp' gradient, the expression would be
ramp_color('blue ramp', 0.75)
You'll also want to use the "scale_linear" function, which makes it easy to scale values from a specified input max/min value to a max/min output range. Let's say your input values are stored in the "population" column and range from 100 to 300, then the expression:
scale_linear( population, 100, 300, 0, 1 )
will return values between 0 and 1, where a population of 100 returns 0 and a population of 300 returns 1, and values between 100 and 300 will return values somewhere between 0 and 1.
Combining these two functions, the expression:
ramp_color('blue ramp', scale_linear(population, 100, 300, 0, 1))
will return a color from the ramp, with its position along the ramp scaling smoothly from population values of 100 to 300.
The legend doesn't reflect data-defined symbology - it only ever shows the "base" symbology as it were, i.e. the individual symbols for each data source which you see in the layers panel.
Since your strat_name matches up to a color_hex value, you may wish to categorise by the color_hex column (to generate the correctly coloured symbology that will appear in the legend), then manually change the labels to reflect the appropriate strat_name column.
You can then save that style so you don't need to keep recreating it every single time.
Best Answer
The reason is that you are not using a data defined size. You are using a graduated size renderer.
To use the data defined size legend as you show in your image, choose another option than graduated size, e.g. graduated color, single symbol, categorized, ... Then click on symbol and on the small box right of "size" option:
Enter your expression or use the assistant. Now you can use the "Data defined size legend".