简体   繁体   English

图例颜色栏被截断,并且使用连续比例限制不受限制

[英]Legend colorbar is truncated and limits not respected with continuous scales

I sometimes have issues with the legend not showing the entire range of colours when using a continuous fill or color scale, eg scale_fill_continuous , scale_fill_gradient , scale_fill_gradientn (and corresponding color scales). 在使用连续fillcolor标时,我有时会遇到图例没有显示整个color范围的问题,例如scale_fill_continuousscale_fill_gradientscale_fill_gradientn (以及相应的color标)。

Specifically the upper range of the colorbar legend is truncated, ie it doesn't extend all the way to the upper limit of the colour palette. 具体地,上部范围colorbar图例被截断,即,它不一直延伸到调色板的上限。 Furthermore, the title of the legend is vertically adjusted to the (truncated) colorbar , and not to the upper limit. 此外,图例的标题垂直调整为(截断的) colorbar ,而不是上限。

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)

在此输入图像描述

Similar for scale_fill_gradientn : 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)

在此输入图像描述

As you can see, although the largest colour value is 0.7 and the point is correctly coloured on the plot with the end of the colour palette, the colorbar is truncated in the upper range - despite the limits are explicitly set - and the title is in the wrong position. 正如你所看到的,尽管最大的色值是0.7,点正确着色与调色板结束的情节, colorbar被截断的区间上限-尽管限制明确设置-和标题是错误的立场。

This problem has occurred on multiple machines and occurs regardless of the palette or theme() options chosen, but I've only ever seen it happen for the upper range. 这个问题在多台机器上发生,无论选择哪个调色板或theme()选项都会发生,但我只看到它发生在上限范围内。 If you vary the upper limit of the colour scale sometimes it works and sometimes it doesn't. 如果你改变颜色标度的上限,它有时会起作用,有时则不起作用。


The following code generates the corresponding issue for color scales: 以下代码为color生成相应的问题:

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 offer some insight? 有人能提供一些见解吗?

This works in the development version of ggplot2 ( 2.2.1.9000 ). 这适用于ggplot22.2.1.9000 )的开发版本。

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)

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

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.title = element_text(size = 18),
        legend.key.size = unit(1.5, "cm"))

p + scale_color_continuous(name = "Title", limits = li, labels = la, breaks = br)

p + scale_color_gradientn(colours = c("black", "white"),
                          name = "Title", limits = li, labels = la, breaks = br)

p + scale_color_gradient(low = "black", high = "white",
                         name = "Title", limits = li, labels = la, breaks = br)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM