【问题标题】:Legend colorbar is truncated and limits not respected with continuous scales图例颜色条被截断,连续比例不遵守限制
【发布时间】:2017-11-10 09:17:45
【问题描述】:

当使用连续的fillcolor 比例时,我有时会遇到图例未显示整个颜色范围的问题,例如scale_fill_continuousscale_fill_gradientscale_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)

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)

如您所见,尽管最大颜色值为 0.7,并且该点在调色板末尾的绘图上正确着色,但 colorbar 在上限范围内被截断 - 尽管明确设置了限制 -并且标题位置错误。

这个问题发生在多台机器上,无论选择何种调色板或theme() 选项都会发生,但我只见过它发生在上限范围内。如果你改变色阶的上限,有时它会起作用,有时它不会。


以下代码为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)

谁能提供一些见解?

【问题讨论】:

  • 你不能用scale_colour_viridis吗?
  • @Axeman 感谢您的建议,我不知道该命令存在。不幸的是,它给了我相同的结果。
  • 只要增加你的限制——哪怕是一点点也行。 limits=c(0.1,0.71),
  • 我刚看到这个“如果你改变色阶的上限,有时它会起作用,有时它不会。”将上限提高一点一直对我有用。
  • 由于您有一个简单的可重现示例,并且这似乎是一个一般的 ggplot 错误,您可以将其作为问题发布在 github 上(在检查之前没有报告过之后)。

标签: r ggplot2 colors


【解决方案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)

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)

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 2021-09-12
    • 2019-01-09
    • 2017-10-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多