[英]`expand` argument in `scale_color_gradient` is ignored
我有一個帶有連續色標的ggplot2
圖,我想刪除高於最大值和低於最小值的額外位。 例如:
set.seed(10)
n = 20
ggplot(data.frame(
x = rnorm(n),
y = rnorm(n),
col = c(0, 1, runif(n - 2)))) +
geom_point(aes(x, y, color = col))
看看色階是如何擴展到略高於 1 和略低於 0 的? 我想擺脫它。 但是expand
似乎被忽略了。 如果我將scale_color_gradient(expand = c(0, 0))
到上面,則沒有明顯的變化。 事實上, scale_color_gradient(expand = c(100, 100))
也沒有區別。 這是一個錯誤嗎?
TLDR
這不是一個錯誤。 增加nbin
在參數guide_colourbar
(即示出了連續的色標映射到GGPLOT2值函數)將移動蜱位置更靠近端部。
解釋
guide_colourbar
將顏色值的范圍渲染到多個bin 中(默認情況下nbin
= 20 個 bin。顯示值范圍的第一個和最后一個刻度分別位於第一個和最后一個 bin 的中點。
下面是不同 nbin 值的一些說明。 我還從默認的raster = TRUE
切換到raster = FALSE
以便更清楚地區分 bin,因為raster = TRUE
帶有插值。
設置數據/基礎圖
set.seed(10)
n = 20
df <- data.frame(x = rnorm(n),
y = rnorm(n),
col = c(0, 1, runif(n - 2)))
# base plot
p <- ggplot(df) +
geom_point(aes(x, y, color = col))
# extreme case: with only 2 bins, the two ticks corresponding to the
# lower & upper limits are positioned in the middle of each rectangles, with
# remaining ticks evenly spaced between them
p + ggtitle("nbin = 2") +
scale_colour_continuous(guide = guide_colourbar(nbin = 2, raster = FALSE))
# as we increase the number of bins, the upper / lower limit ticks move closer
# to the respective ends
p + ggtitle("nbin = 4") +
scale_colour_continuous(guide = guide_colourbar(nbin = 4, raster = FALSE))
p + ggtitle("nbin = 10") +
scale_colour_continuous(guide = guide_colourbar(nbin = 10, raster = FALSE))
# nbin = 20 is the default value; at this point, the upper / lower limit ticks
# are relatively close to the ends, but still distinct
p + ggtitle("nbin = 20") +
scale_colour_continuous(guide = guide_colourbar(nbin = 20, raster = FALSE))
# with 50 bins, the upper / lower limit ticks move closer to the ends, and
# the stacked rectangles are so thin that raster = FALSE doesn't really have
# much effect from here onwards; we can't visually distinguish the individual
# rectangles anymore
p + ggtitle("nbin = 50") +
scale_colour_continuous(guide = guide_colourbar(nbin = 50, raster = FALSE))
# with 100 bins, the upper / lower limit ticks are even closer
p + ggtitle("nbin = 100") +
scale_colour_continuous(guide = guide_colourbar(nbin = 100, raster = FALSE))
# with 500 bins, the upper / lower limits are practically at the ends now
p + ggtitle("nbin = 500") +
scale_colour_continuous(guide = guide_colourbar(nbin = 500, raster = FALSE))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.