简体   繁体   English

`scale_color_gradient` 中的 `expand` 参数被忽略

[英]`expand` argument in `scale_color_gradient` is ignored

I have a ggplot2 plot with a continuous color scale and I want to remove the extra bits above the maximum and below the minimum.我有一个带有连续色标的ggplot2图,我想删除高于最大值和低于最小值的额外位。 For example:例如:

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))

糟糕的情节

See how the color scale extends a little above 1 and a little below 0?看看色阶是如何扩展到略高于 1 和略低于 0 的? I want to get rid of that.我想摆脱它。 But expand seems to be ignored.但是expand似乎被忽略了。 If I add scale_color_gradient(expand = c(0, 0)) to the above, there's no visible change.如果我将scale_color_gradient(expand = c(0, 0))到上面,则没有明显的变化。 In fact, scale_color_gradient(expand = c(100, 100)) makes no difference, either.事实上, scale_color_gradient(expand = c(100, 100))也没有区别。 Is this a bug?这是一个错误吗?

TLDR TLDR

It's not a bug.这不是一个错误。 Increasing the nbin parameter in guide_colourbar (the function that shows continuous colour scales mapped onto values in ggplot2) will move the tick positions closer to the ends.增加nbin在参数guide_colourbar (即示出了连续的色标映射到GGPLOT2值函数)将移动蜱位置更靠近端部。

Explanation解释

guide_colourbar renders the range of colour values into a number of bins (with nbin = 20 bins by default. The first & last ticks showing the range of values are positioned at the midpoint of the first and last bins respectively. guide_colourbar将颜色值的范围渲染到多个bin 中(默认情况下nbin = 20 个 bin。显示值范围的第一个和最后一个刻度分别位于第一个和最后一个 bin 的中点。

Below are some illustrations for different nbin values.下面是不同 nbin 值的一些说明。 I also switched from the default raster = TRUE to raster = FALSE for clearer distinction between bins, as raster = TRUE comes with interpolation.我还从默认的raster = TRUE切换到raster = FALSE以便更清楚地区分 bin,因为raster = TRUE带有插值。

Setting up data / base plot设置数据/基础图

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM