简体   繁体   中英

`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. 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? I want to get rid of that. But expand seems to be ignored. If I add scale_color_gradient(expand = c(0, 0)) to the above, there's no visible change. In fact, scale_color_gradient(expand = c(100, 100)) makes no difference, either. Is this a bug?

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.

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.

Below are some illustrations for different nbin values. I also switched from the default raster = TRUE to raster = FALSE for clearer distinction between bins, as raster = TRUE comes with interpolation.

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

插图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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