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

F. *_*ans 7 r colors ggplot2

我有时有问题与使用连续当图例不显示颜色的整个范围fillcolor尺度,例如scale_fill_continuous,scale_fill_gradient,scale_fill_gradientn(和相应的color尺度).

特别是colorbar图例的上限范围被截断,即它不会一直延伸到调色板的上限.此外,图例的标题垂直调整为(截断的)colorbar,而不是上限.

一个小例子:

# 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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

类似于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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

正如您所看到的,虽然最大的颜色值是0.7,并且在带有调色板末尾的绘图上正确地着色点,但是colorbar在上限范围内被截断 - 尽管明确设置了限制 - 并且标题位于错误的立场.

这个问题在多台机器上发生,无论theme()选择哪个调色板或选项都会发生,但我只看到它发生在上限范围内.如果你改变颜色标度的上限,它有时会起作用,有时则不起作用.


以下代码为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)
Run Code Online (Sandbox Code Playgroud)

有人能提供一些见解吗?

Hen*_*rik 1

ggplot2这适用于( )的开发版本2.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)
Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

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

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