带有分隔符而不是刻度的连续颜色条 [英] Continuous color bar with separators instead of ticks

查看:92
本文介绍了带有分隔符而不是刻度的连续颜色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片应该很好地说明我想要实现的目标.理想情况下,所得的色条将保持连续的性质,即应基于连续的美学而不是离散的美学,或者至少非常接近以使其表现出类似的行为.

The picture should give a good idea of what I would want to achieve. Ideally, the resulting colour bar would keep a continuous nature, i.e., should be based on a continuous rather than discrete aesthetic, or at least come very close to behave like it.

这还意味着,不仅仅是将离散填充的图例键足够靠近的简单方法.

This also means, not just the simple approach of just bringing the legend keys of a discrete fill close enough together.

是的,我可以创建一个假的图例,但是我想要一个在图例绘图级别进行修改的解决方案

Yes, I could create a fake legend, but I'd like a solution that modifies at the legend drawing level.

我也会对有趣的网格黑客感到非常满意!

I would be also very happy with interesting grid hacks!

library(ggplot2)

ggplot(iris, aes(Sepal.Length, y = Sepal.Width, fill = Petal.Length))+
  geom_point(shape = 21) +
  scale_fill_continuous() +
  guides(fill = guide_colorbar(ticks.colour = "black"))

这是photoshop的,很抱歉!我不想创建箭头,只是创建带有分隔符而不是刻度的连续刻度

最终目标是将颜色条复制为在此主题中最初提出的

The ultimate goal is to reproduce a color bar as orignially proposed in this thread

但是,这与创建离散渐变条无关,而仅与分隔符有关!

However, this is not about creating the discrete gradient bar, this is just about the separators!

离散梯度条不是问题,已经有了解决方案(例如, https://stackoverflow.com/a /62544405/7941188 https://stackoverflow.com/a/62556763/7941188 https://stackoverflow.com/a/50540633/7941188 )

The discrete gradient bar is not the problem, there are already solutions out there (e.g., https://stackoverflow.com/a/62544405/7941188, https://stackoverflow.com/a/62556763/7941188, https://stackoverflow.com/a/50540633/7941188)

推荐答案

在ggplot v3.3.0中引入的可扩展指南系统中肯定有一个选项.请参见下面的示例:

There definitely is an option with the extendable guide system introduced in ggplot v3.3.0. See example below:

library(ggplot2)

guide_longticks <- function(...) {
  guide <- guide_colorbar(...)
  class(guide) <- c("guide", "guide_longticks", "colorbar")
  guide
}

guide_gengrob.guide_longticks <- function(guide, theme) {
  dir <- guide$direction
  guide <- NextMethod()
  is_ticks <- grep("^ticks$", guide$layout$name)
  ticks <- guide$grobs[is_ticks][[1]]
  if (dir == "vertical") {
    ticks$x1 <- rep(tail(ticks$x1, 1), length(ticks$x1))
  } else {
    ticks$y1 <- rep(tail(ticks$y1, 1), length(ticks$y1))
  }
  
  guide$grobs[[is_ticks]] <- ticks
  guide
}

ggplot(iris, aes(Sepal.Length, y = Sepal.Width, fill = Petal.Length))+
  geom_point(shape = 21) +
  scale_fill_continuous() +
  guides(fill = guide_longticks(ticks = TRUE, ticks.colour = "black"))

reprex软件包(v0.3.0)创建于2020-06-24 sup>

Created on 2020-06-24 by the reprex package (v0.3.0)

如果要在刻度之间使用纯色,也可以尝试使用此替代构造函数:

Also try this alternative constructor if you want flat colours in between ticks:

guide_longticks <- function(...) {
  guide <- guide_colorsteps(...)
  class(guide) <- c("guide", "guide_longticks", "colorsteps", "colorbar")
  guide
}

如果您想要更清晰的矢量文件,则以下gengrob函数还将删除较小的滴答声.有点假设它们是刻度线的最后一半:

The following gengrob function would also delete the smaller ticks, if you want cleaner vector files. It kind of assumes that they are the last half of the ticks though:

guide_gengrob.guide_longticks <- function(guide, theme) {
  dir <- guide$direction
  guide <- NextMethod()
  is_ticks <- grep("^ticks$", guide$layout$name)
  ticks <- guide$grobs[is_ticks][[1]]
  n <- length(ticks$x0)
  if (dir == "vertical") {
    ticks$x0 <- head(ticks$x0, n/2)
    ticks$x1 <- rep(tail(ticks$x1, 1), n/2)
  } else {
    ticks$y0 <- head(ticks$y0, n/2)
    ticks$y1 <- rep(tail(ticks$y1, 1), n/2)
  }
  
  guide$grobs[[is_ticks]] <- ticks
  guide
}

这篇关于带有分隔符而不是刻度的连续颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆