如何在 ggplot guide_colorsteps 中自定义标签?
How can I customize labels in ggplot guide_colorsteps?
这个示例代码提供了我想要的一切,除了颜色栏标签。
require(ggplot2)
require(RColorBrewer)
df <- faithfuld
pbase <- ggplot(data=df, aes(x=waiting, y=eruptions))
p1 <- pbase + geom_contour_filled(aes(z=100*density),
show.legend=T) +
scale_fill_manual(values=brewer.pal(11,"Spectral"),
guide = guide_colorsteps(direction="horizontal",
title.position="top",
title="Density*100.")) +
theme(legend.position="top")
我想要的不是默认标签,而是位于栏端点的标签。我试过使用指南中的“draw.ulim”和“draw.llim”参数,但它们似乎没有效果。我搜索过类似的帖子,但没有找到这个问题的答案。
不是一个完美的解决方案,但也许它符合您的需求:
- 要显示端点,请按照我在评论中的建议添加
show.limits=TRUE
。
- 为了摆脱中间标签,我使用了自定义标签功能。该函数被秤调用了两次。一次用于默认中间中断(在几乎(!!)所有情况下都是长度 > 2 的向量),一次用于限制(这是一个长度为 2 的向量)。因此我检查传递的向量的长度并只保留“限制”的标签。但请记住,这只是一种启发式方法,在极端特殊情况下可能会失败。
require(ggplot2)
#> Loading required package: ggplot2
require(RColorBrewer)
#> Loading required package: RColorBrewer
df <- faithfuld
pbase <- ggplot(data=df, aes(x=waiting, y=eruptions))
fun_lab <- function(x) {
if (length(x) == 2) x else ""
}
p1 <- pbase + geom_contour_filled(aes(z=100*density),show.legend=T) +
scale_fill_manual(values=brewer.pal(11,"Spectral"),
labels = fun_lab,
guide = guide_colorsteps(direction="horizontal",
title.position="top",
title="Density*100.", show.limits = TRUE)) +
theme(legend.position="top")
p1
这个示例代码提供了我想要的一切,除了颜色栏标签。
require(ggplot2)
require(RColorBrewer)
df <- faithfuld
pbase <- ggplot(data=df, aes(x=waiting, y=eruptions))
p1 <- pbase + geom_contour_filled(aes(z=100*density),
show.legend=T) +
scale_fill_manual(values=brewer.pal(11,"Spectral"),
guide = guide_colorsteps(direction="horizontal",
title.position="top",
title="Density*100.")) +
theme(legend.position="top")
我想要的不是默认标签,而是位于栏端点的标签。我试过使用指南中的“draw.ulim”和“draw.llim”参数,但它们似乎没有效果。我搜索过类似的帖子,但没有找到这个问题的答案。
不是一个完美的解决方案,但也许它符合您的需求:
- 要显示端点,请按照我在评论中的建议添加
show.limits=TRUE
。 - 为了摆脱中间标签,我使用了自定义标签功能。该函数被秤调用了两次。一次用于默认中间中断(在几乎(!!)所有情况下都是长度 > 2 的向量),一次用于限制(这是一个长度为 2 的向量)。因此我检查传递的向量的长度并只保留“限制”的标签。但请记住,这只是一种启发式方法,在极端特殊情况下可能会失败。
require(ggplot2)
#> Loading required package: ggplot2
require(RColorBrewer)
#> Loading required package: RColorBrewer
df <- faithfuld
pbase <- ggplot(data=df, aes(x=waiting, y=eruptions))
fun_lab <- function(x) {
if (length(x) == 2) x else ""
}
p1 <- pbase + geom_contour_filled(aes(z=100*density),show.legend=T) +
scale_fill_manual(values=brewer.pal(11,"Spectral"),
labels = fun_lab,
guide = guide_colorsteps(direction="horizontal",
title.position="top",
title="Density*100.", show.limits = TRUE)) +
theme(legend.position="top")
p1