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

时间:2017-06-08 15:07:36

标签: r ggplot2 colors

在使用连续fillcolor比例时,我有时会遇到图例没有显示整个颜色范围的问题,例如scale_fill_continuousscale_fill_gradient,{{1 (和相应的scale_fill_gradientn尺度)。

具体地说,color图例的上限范围被截断,即它不会一直延伸到调色板的上限。此外,图例的标题垂直调整为(截断的)colorbar,而不是上限。

一个小例子:

colorbar

enter image description here

类似于# 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)

scale_fill_gradientn

enter image description here

正如您所看到的,尽管最大的颜色值为0.7,并且在调色板末端的点上正确地着色了点,但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) 在上限范围内被截断 - 尽管明确限制设置 - 标题位置错误。

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

以下代码为theme()比例生成相应的问题:

color

任何人都可以提供一些见解吗?

1 个答案:

答案 0 :(得分:1)

这适用于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)