Currently, a continuous colour bar legend, guide_colorbar
is available only with scale_fill
and scale_colour
, and not with scale_alpha
. The legend which is generated with scale_alpha
is of a discrete type (guide_legend
).
A small example where color
and alpha
are mapped to a continuous variable:
scale_color
generates a continuous color bar type legend :
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Sepal.Width)) +
geom_point()
scale_alpha
generates a discrete legend, despite alpha
is mapped to a continuous variable:
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, alpha = Sepal.Width)) +
geom_point()
Is there some way to get a continuous color bar legend also for scale_alpha
?
Now, in 'Composer Manager', use 'Add Label' and 'Add Image' to add continuous color ramp along with legend. To get the 'Max' and 'Min' value for continuous color ramp use, Layer Properties -> Style.
Under symbol, reduce the height of the symbols (1), deselect the ‘draw stroke for raster symbol’ (2), and under spacing, increase the spacing between symbols (3). Place a rectangle next to the legend, and select as style gradient fill. Select the colour ramp, and then the option to edit it.
Add a legend with limited number of discrete classes (enough to capture the gradient). In the composer, add the raster layer and legend. Under symbol, reduce the height of the symbols (1), deselect the ‘draw stroke for raster symbol’ (2), and under spacing, increase the spacing between symbols (3).
The arguments to the colorbar call are the ScalarMappable (constructed using the norm and cmap arguments), the axes where the colorbar should be drawn, and the colorbar's orientation. For more information see the colorbar API. The second example shows how to make a discrete colorbar based on a continuous cmap.
The default minimum alpha for scale_alpha_continuous
is 0.1, and the max is 1. I wrote this assuming that you might adjust the minimum to be more visible, but you'd keep the max at 1.
First I set amin
to that default of 0.1, and the chosen colour for the points as highcol
. Then we use the col2rgb
to make a matrix of the RGB values and blend them with white, as modified from this answer written in C#. Note that we're blending with white, so you should be using a theme that has a white background (e.g. theme_classic()
as below). Finally we convert that matrix to hex values and paste it into a single string with #
in front for standard RGB format.
require(scales)
amin <- 0.1
highcol <- hue_pal()(1) # or a color name like "blue"
lowcol.hex <- as.hexmode(round(col2rgb(highcol) * amin + 255 * (1 - amin)))
lowcol <- paste0("#", sep = "",
paste(format(lowcol.hex, width = 2), collapse = ""))
Then we plot as you might be planning to already, with your variable of choice set to the alpha
aesthetic, and here some geom_point
s. Then we plot another layer of points, but with colour set to that same variable, and alpha = 0
(or invisible). This gives us our colourbar we need. Then we have to set the range of scale_colour_gradient
to our colours from above.
ggplot(iris, aes(Sepal.Length, Sepal.Width, alpha = Petal.Length)) +
geom_point(colour = highcol, size = 3) +
geom_point(aes(colour = Petal.Length), alpha = 0) +
scale_colour_gradient(high = highcol, low = lowcol) +
guides(alpha = F) +
labs(colour = "Alpha\nlabel") +
theme_classic()
I'm guessing you most often would want to use this with only a single colour, and for that colour to be black. In that simplified case, replace highcol
and lowcol
with "black" and "grey90". If you want to have multiple colours, each with an alpha varied by some other variable... that's a whole other can of worms and probably not a good idea.
If you replace colour with fill for my solution above, you can still use colour as an aesthetic. Here I used highcol <-hue_pal()(3)[2]
to extract that default green colour.
ggplot(aes(Sepal.Length, Sepal.Width, alpha = Petal.Length)) +
geom_point(aes(colour = Species), size = 3) +
geom_point(aes(fill = Petal.Length), alpha = 0) +
scale_fill_gradient(high = highcol, low = lowcol) +
guides(alpha = F) +
labs(fill = "Petal\nLength") +
theme_classic()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With