我有一个unknown
图,具有连续的色标,我想删除最大值以上和最小值以下的多余位。例如:
ggplot2
看看色标是如何延伸到略高于1并略低于0的?我想摆脱它。但是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))
似乎被忽略了。如果我在上面添加expand
,则没有明显的变化。实际上,scale_color_gradient(expand = c(0, 0))
也没有区别。这是错误吗?
答案 0 :(得分:1)
TLDR
这不是错误。增大nbin
中的guide_colourbar
参数(该函数显示将连续的色标映射到ggplot2中的值)将使刻度位置更靠近末端。
说明
guide_colourbar
将颜色值的范围渲染为许多 bins (默认情况下,nbin
= 20 bins。显示值范围的第一个和最后一个刻度是分别位于第一个和最后一个容器的中点。
下面是不同nbin值的一些说明。我还从默认的raster = TRUE
切换到raster = FALSE
,以使各个垃圾箱之间的区别更加清晰,因为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))