gpt4 book ai didi

r - 图例颜色条被截断并且连续比例不遵守限制

转载 作者:行者123 更新时间:2023-12-02 15:17:31 25 4
gpt4 key购买 nike

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

enter image description here

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

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

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

<小时/>

以下代码生成颜色比例的相应问题:

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)
<小时/>

有人可以提供一些见解吗?

最佳答案

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

关于r - 图例颜色条被截断并且连续比例不遵守限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44439423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com