我根据这样的一段代码绘制了一张地图:
ggplot(faithfuld, aes(y=eruptions, x=waiting, z=100*density)) +
geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))+
theme(plot.title = element_text(size = 10,hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)
这是我的情节目前看起来像:
但是我的老板让我把这个传奇变成这样:
或者像这样:
此链接 ( https://ggplot2.tidyverse.org/reference/theme.html ) 中的参数只是为图例提供了细微的更改。而且我找不到任何可以实现这一点的论据,ggplot 是否可行?或者我必须使用其他绘图包?
创建具有不同间隔宽度且图例级别之间没有间距的离散颜色条这个问题(答案第 4 号)提供了一种方法,可以创建像我的老板要求的颜色条,但是,我正在使用geom_contour_filled(breaks = c(-Inf,-2., -1.5, -1., -0.5, 0, 0.5, 1, 1.5, 2, 3, 4, 5, 7, 9, 11,Inf))此参数,因此图例始终与大量文字:
有什么解决办法吗?
我相信这与我之前的答案足以证明第二个答案是正确的。我回答后者完全否认 ggplot2 3.3.0 附带的新比例函数,现在我们开始,它们使它变得更容易。我仍然会保留其他解决方案,因为它可能有助于......非常具体的要求。
我们仍然需要使用 metR,因为连续/离散轮廓的问题仍然存在,而 metR::geom_contour_fill 可以很好地处理这个问题。
我正在修改该scale_fill_fermenter函数,这是在这里使用的好函数,因为它适用于分级比例。我稍微增强了底层brewer_pal功能,因此它提供的颜色比原来的啤酒颜色更多,如果n > max(palette_colors).
update
您应该用来guide_colorsteps更改颜色条。请参阅 有关酒吧开始和结束时较长休息时间的相关讨论。
library(ggplot2)
library(metR)
mybreaks <- c(seq(-2,2,0.5), 3:5, seq(7,11,2))
ggplot(faithfuld, aes(eruptions, waiting)) +
metR::geom_contour_fill(aes(z = 100*density)) +
scale_fill_craftfermenter(
breaks = mybreaks,
palette = "Spectral",
limits = c(-2,11),
guide = guide_colorsteps(
frame.colour = "black",
ticks.colour = "black", # you can also remove the ticks with NA
barwidth=20)
) +
theme(legend.position = "bottom")
#> Warning: 14 colours used, but Spectral has only 11 - New palette created based
#> on all colors of Spectral
Run Code Online (Sandbox Code Playgroud)

## with uneven steps, better representing the scale
ggplot(faithfuld, aes(eruptions, waiting)) +
metR::geom_contour_fill(aes(z = 100*density)) +
scale_fill_craftfermenter(
breaks = mybreaks,
palette = "Spectral",
limits = c(-2,11),
guide = guide_colorsteps(
even.steps = FALSE,
frame.colour = "black",
ticks.colour = "black", # you can also remove the ticks with NA
barwidth=20, )
) +
theme(legend.position = "bottom")
#> Warning: 14 colours used, but Spectral has only 11 - New palette created based
#> on all colors of Spectral
Run Code Online (Sandbox Code Playgroud)

功能修改
craftbrewer_pal <- function (type = "seq", palette = 1, direction = 1)
{
pal <- scales:::pal_name(palette, type)
force(direction)
function(n) {
n_max_palette <- RColorBrewer:::maxcolors[names(RColorBrewer:::maxcolors) == palette]
if (n < 3) {
pal <- suppressWarnings(RColorBrewer::brewer.pal(n, pal))
} else if (n > n_max_palette){
rlang::warn(paste(n, "colours used, but", palette, "has only",
n_max_palette, "- New palette created based on all colors of",
palette))
n_palette <- RColorBrewer::brewer.pal(n_max_palette, palette)
colfunc <- grDevices::colorRampPalette(n_palette)
pal <- colfunc(n)
}
else {
pal <- RColorBrewer::brewer.pal(n, pal)
}
pal <- pal[seq_len(n)]
if (direction == -1) {
pal <- rev(pal)
}
pal
}
}
scale_fill_craftfermenter <- function(..., type = "seq", palette = 1, direction = -1, na.value = "grey50", guide = "coloursteps", aesthetics = "fill") {
type <- match.arg(type, c("seq", "div", "qual"))
if (type == "qual") {
warn("Using a discrete colour palette in a binned scale.\n Consider using type = \"seq\" or type = \"div\" instead")
}
binned_scale(aesthetics, "fermenter", ggplot2:::binned_pal(craftbrewer_pal(type, palette, direction)), na.value = na.value, guide = guide, ...)
}
Run Code Online (Sandbox Code Playgroud)