The color scale of legends is truncated, and restrictions are not respected with continuous scales

I sometimes have problems with a legend that does not display the entire range of colors when using a continuous fill or color scale, such as scale_fill_continuous , scale_fill_gradient , scale_fill_gradientn (and the corresponding color scales) ,.

In particular, the upper range of the colorbar legend colorbar truncated, i.e. it does not extend to the upper limit of the color palette. In addition, the name of the legend is vertically adjusted to the (truncated) colorbar , and not to the upper limit.

A small example:

 # set up data, base plot, limits, labels and breaks # adjust theme for legend just to make the issue more visible df <- data.frame(x = 1:4, y = 1, col = c(0.1, 0.3, 0.5, 0.7)) library(ggplot2) p <- ggplot(data = df, aes(x, y, fill = col)) + geom_point(size = 15, shape = 21) + theme_minimal() + theme(legend.background = element_rect(fill = "grey60"), legend.text = element_text(size = 18), legend.title = element_text(size = 18), legend.key.size = unit(1.5, "cm")) li <- c(0.1, 0.7) la <- seq(0.1, 0.7, 0.2) br <- seq(0.1, 0.7, 0.2) p + scale_fill_continuous(name = "Title", limits = li, labels = la, breaks = br) 

enter image description here

Similarly for scale_fill_gradientn :

 p + scale_fill_gradientn(colours = c("black", "white"), name = "Title", limits = li, labels = la, breaks = br) # and scale_fill_gradient # p + scale_fill_gradient(low = "black", high = "white", # name = "Title", limits = li, labels = la, breaks = br) 

enter image description here

As you can see, although the highest color value is 0.7, and the dot is correctly colored on the graph with the end of the color palette, the colorbar truncated in the upper range - despite the fact that the limits are clearly set - and the name is in the wrong position.

This problem arose on several machines and occurs regardless of the selected palette or theme() , but I only saw it in the upper range. If you change the upper limit of the color scale, sometimes it works, and sometimes not.


The following code generates the corresponding problem for the color scales:

 p <- ggplot(data = df, aes(x, y, color = col)) + geom_point(size = 15) + theme_minimal() + theme(legend.background = element_rect(fill = "grey60"), legend.text = element_text(size = 18), legend.key.size = unit(1.5, "cm")) p + scale_color_continuous(limits = li, labels = la, breaks = br) p + scale_color_gradientn(colours = c("black", "white"), limits = li, labels = la, breaks = br) p + scale_color_gradient(low = "black", high = "white", limits = li, labels = la, breaks = br) 

Can anyone give an idea?

+7
colors r ggplot2
source share

No one has answered this question yet.

See related questions:

8
Manually expanding the range of legends in geom_tile
7
Several legends with ggplot2
4
How to change data binding method for geom_hex ()?
3
Difference between transfer options in aes () and beyond in ggplot2
3
Legend Trouble in R - how to change legend text in ggplot2
2
How to change color legend in fine granularity using ggplot2
2
ggplot2 with black outline
0
Using ggplot to build a map from a matrix
0
how to make ggplot place color labels in the middle of each part of the palette?
0
Adjust the title on the graph (ggplot2) with the label of another file

All Articles