More about color scales
1. More about color scales
As well as controlling the set of colors variables are mapped to, the palette, we can also control how the numbers are mapped to the colors.2. Mapping of numbers to color
You might have noticed a slight difference in the way ggplot2 and tmap handle this mapping. In ggplot2, when color is mapped to a continuous variable, values are mapped to a continuous color gradient. Here's the legend for our predicted house price plot. If the predicted value of a cell is 250000, its color would be somewhere between the purple assigned to 260000 and the purple-blue assigned to 240000. In contrast in tmap values are mapped to a discrete set of colors. In our plot of the population proportions, if a cell has a proportion of point-5, it gets exactly the same color as a cell with a proportion of point-45 or point-55. Controlling the mapping of a continuous color scale is usually achieved by transforming the scale, whereas with a discrete color scale, we control the mapping by binning the variable.3. Discrete vs. continuous mapping
Continuous scales are useful because we can attempt to have values numerically the same distance apart appear using colors we perceive as the same distance apart, a property known as perceptually uniform. However, discrete scales give us more control over how values are mapped to colors if this uniform mapping doesn't work. They are also generally easier to use to look up values. For example, people find it easier to identify the exact range of a value using discrete set of colors rather than a continuous gradient.4. Cutting a variable into bins (equal)
tmap provides some shortcuts for binning a variable using the classInt package. You'll see these shortcuts in the following exercises, so let's focus on how the methods of binning here. The classInt package provides a variety of methods for cutting a variable into intervals. Here, values is just a vector of predicted prices from our house sales heat map. We can use the classIntervals function to calculate interval cut points and return a table of how many observations fell in each interval. The method equal does perhaps the easiest thing: it cuts the range of the variable into equal sized intervals. Here, we ask for 5 intervals. If you look at a histogram of the prices, we've colored each price based on this binning. You can see we have cut the x-axis evenly and prices in the same interval get the same color. This is perhaps the most intuitive but it can fail if your variable varies over orders of magnitude.5. Cutting a variable into bins
The method quantile instead picks intervals so that the number of observations in each interval is roughly the same, here exactly the same, with 320 in each interval. In our heat map, we'd end up with the same number of cells of each color.6. Cutting a variable into bins
Neither equal nor quantile guarantees the breaks between intervals happen at nice round numbers. The pretty method attempts to do this, with the caveat that you might not get exactly the number of intervals you requested.7. Cutting a variable into bins
And finally, the fixed method allows you to specify your own breakpoints.8. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.