2

How do you make an unclassed choropleth in QGIS 2.x? I'm using 2.4 and can't find an option in a layer's Style tab like the continuous color option that seems to have existed in QGIS 1.x.

I'm curious to see ways to work around this if it is indeed no longer an official option.

1

You can accomplish this using data-defined properties (as pointed out in comment by underdark). You need to generate a string in the format '<red>, <green>, <blue>, <alpha>, where each slot is a number from 0 to 255. A "pure" red would be represented as '255, 0, 0, 255'. In practice, alpha can be omitted unless you want some degree of transparency, and I will omit it in the rest of my examples.

In order to access data-defined properties, go to the Style tab of the Layer Properties dialog and choose Single Symbol (the default for a new layer). On the left you will see a list of Symbol layers. Click on Simple fill. When you do, you will see the Symbol layer properties on the right. Click on the Data defined properties button:

enter image description here

In the Data defined properties dialog, check Color and click the button labelled ... in the Expressions column:

enter image description here

Now you're in the Expression string builder and need to construct your string representing the RGB color. Convention dictates that we should use a high-value, low-saturation color (white, pale gray, other light and/or dull colors) to represent low data values and low-value (i.e. dark), high-saturation colors to indicate high data values. In the simplest case of a white-black color ramp, we want low data values to map to '255, 255, 255' and high data values to map to '0, 0, 0'. An expression which would accomplish this is:

tostring(255 - 255 * "<field_name>" / <max_field_value>) || ', ' 
|| tostring(255 - 255 * "<field_name>" / <max_field_value>) || ', ' 
|| tostring(255 - 255 * "<field_name>" / <max_field_value>)

As far as I know you can't use aggregate functions in expression builders, so you will have to manually insert the maximum value for the field in place of <max_field_value>. (Alternatively, add a column with the maximum value repeated for all records, or use a window function in PostGIS.)

If your lowest value isn't 0, it may be easier to use the scale_linear function:

tostring(toint(scale_linear("<field_name>", 0, <max_field_value>, 255, 0))) || ', ' 
|| tostring(toint(scale_linear("<field_name>", 0, <max_field_value>, 255, 0))) || ', ' 
|| tostring(toint(scale_linear("<field_name>", 0, <max_field_value>, 255, 0)))

Another advantage is that by reversing the output range, you can get low data values at 255 and high data values at 0 without having to use the 255 - <expression> trick that I used in the previous iteration.

To get colors, it will be easiest to work with the pure colors of red, green, blue, cyan, magenta, and yellow. In this case, leave one or two values pinned at 255 and allow the other one or two to vary linearly with the data. For example, to get an unclassed choropleth ramped from white to magenta, keep red and blue pinned at 255:

'255 , ' || tostring(toint(scale_linear(dp0010001, 0, 9818605, 255, 0))) || ', 255'

To get more complex ramps, you're going to have to fool around with the math to get the colors where you want.

As an aside, as underdark points out, you can accomplish much the same result by using Graduated Colors with a large number of categories and using the Equal Interval setting. Anything above 50 categories will probably be indistinguishable from an unclassed choropleth. If you want, QGIS allows you to go up to 999 categories.

  • 1
    It's worth noting the more convenient solution using the color_ramp function presented by @ndawson in gis.stackexchange.com/questions/104706/… – underdark Sep 7 '14 at 21:54
  • That definitely looks easier. I was trying to use that function but using 0-255 scale instead of 0-1, and not getting the desired output. – Lee Hachadoorian Sep 7 '14 at 23:17
  • This answer and the comment by @underdark got me to a pretty reasonable solution along the lines of using ramp_color('RdPu', "<field_name>" / <max_field_value>) in Data defined properties. Thanks! – eric brelsford Sep 8 '14 at 4:57
2

If I understand you correctly, you are looking for the graduated renderer. The dialog looks slightly different in 2.4 but the docs show how it looks like in 2.2:

enter image description here

You can add more classes but after a certain point, viewers won't be able to tell the colors apart anyway. An alternative are data-defined properties which allow you to control the color (and other properties) in detail. The disadvantage of data-defined properties is that QGIS is currently not able to provide a legend for data-defined styles. If you need one, you have to create it manually.

  • Yes, the graduated renderer creates classed choropleths (puts the features in "buckets"), but I'm looking for a way to make an unclassed choropleth (without buckets). So the features would receive a color according to where they fall on a continuous color scale. – eric brelsford Sep 6 '14 at 20:13
  • 1
    you can add more classes but after a certain point, viewers won't be able to tell the colors apart anyway. An alternative are data-defined properties which allow you to control the color (and other properties) in detail. – underdark Sep 6 '14 at 20:52
  • 2
    Unclassed choropleths are a defensible choice and are covered in standard texts such as Thematic Cartography & Geovisualization, 3e by Slocum, et al. – Lee Hachadoorian Sep 7 '14 at 20:17
  • Thanks, it's good to know that QGIS doesn't have a nice way to make a legend for data-defined styles. I could have been more clear in my question: I know unclassed choropleths aren't without their downsides, but I think they can be useful. – eric brelsford Sep 8 '14 at 4:54

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.