如何避免选择类似的颜色以进行填充和颜色尺度?

发布于 2025-01-20 15:47:24 字数 2436 浏览 3 评论 0原文

上下文:我想在同一绘图中使用 viridis 调色板作为色标和填充比例。我使用的几何图形并不都有 colorfill 参数,因此不可能将所有内容“混合”到一个比例中。换句话说,我想使用相同的调色板,但确保该调色板中的每种颜色仅跨比例使用一次,同时保持 viridis 调色板的属性(打印和色盲友好)。

问题:当使用scale_color_viridis_d()scale_fill_viridis_d()时,我最终会在填充和色阶之间共享颜色,这使得最终的结果情节难以阅读。

问题:是否可以告诉 ggplot 在 vi​​ridis 调色板中跨填充和色阶选择不同的颜色?

library(ggplot2)
# example dataset
zones <- data.frame(starts = c(1, 3, 4),
                    ends = c(2, 3.75, 5),
                    item = rep("A", 3))
# current plot, note that the same color from viridis scale is picked
ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  scale_color_viridis_d() +
  scale_fill_viridis_d(alpha = 0.5)

预期输出:来自同一调色板的不同颜色用于填充和色阶。在这里,我使用了来自 scale_*_viridis_d() 函数的 begin =end = 参数,但我觉得这不是最佳选择,因为许多颜色都是“像这样分割调色板就丢失了”。

# look of the expected output
ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  scale_color_viridis_d(begin = 0.5, end = 1) +
  scale_fill_viridis_d(alpha = 0.5, begin = 0, end = 0.49)

[编辑] 我使用 scale_fill_viridis_d()aestheticsguide 参数更近了一步,但它弄乱了图例:

ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  # scale_color_viridis_d() +
  scale_fill_viridis_d(alpha = 0.5, 
                       aesthetics = c("color", "fill"), 
                       guide = guide_legend("Highlights")) 

reprex 包 (v2.0.1)

Context: I would like to use viridis color palette for a color scale and a fill scale within the same plot. The geoms I use do not all have a color or a fill argument so it is not possible to "blend" everything into one scale. In other words, I want to use the same palette but make sure that each color from this palette is used only once across scales, while keeping the properties of the viridis palette (printing and colorblind friendly).

Problem: when using scale_color_viridis_d() and scale_fill_viridis_d() I end up with colors being shared across the fill and the color scale which make the final plot difficult to read.

Question: is it possible to tell ggplot to pick different colors within the viridis palette across the fill and the color scales?

library(ggplot2)
# example dataset
zones <- data.frame(starts = c(1, 3, 4),
                    ends = c(2, 3.75, 5),
                    item = rep("A", 3))
# current plot, note that the same color from viridis scale is picked
ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  scale_color_viridis_d() +
  scale_fill_viridis_d(alpha = 0.5)

Expected output: different colors from the same palette being used for the fill and the color scales. Here I used begin = and end = arguments from the scale_*_viridis_d() functions but I feel this is not optimal as many colors are "lost" by spliting the palette like this.

# look of the expected output
ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  scale_color_viridis_d(begin = 0.5, end = 1) +
  scale_fill_viridis_d(alpha = 0.5, begin = 0, end = 0.49)

[EDIT]
I got a step closer using aesthetics and guide arguments of scale_fill_viridis_d() but it messes the legend:

ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  # scale_color_viridis_d() +
  scale_fill_viridis_d(alpha = 0.5, 
                       aesthetics = c("color", "fill"), 
                       guide = guide_legend("Highlights")) 

Created on 2022-04-12 by the reprex package (v2.0.1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2025-01-27 15:47:24

我想最简单的方法是以相反的顺序获得 viridis 颜色。通常它是从深紫色到黄色,使用direction = -1参数,我们可以让它从黄色到深紫色。

library(ggplot2)
zones <- data.frame(starts = c(1, 3, 4),
                    ends = c(2, 3.75, 5),
                    item = rep("A", 3))

ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  scale_color_viridis_d(direction = -1) +
  scale_fill_viridis_d(alpha = 0.5)

reprex 包 (v2.0.1)

I guess the simplest way would be to get the viridis color in reverse order. Normally it's from deep purple to yellow, with the direction = -1 argument, we can have it from yellow to deep purple.

library(ggplot2)
zones <- data.frame(starts = c(1, 3, 4),
                    ends = c(2, 3.75, 5),
                    item = rep("A", 3))

ggplot(data = zones) +
  geom_rect(aes(xmin = starts, xmax = ends, ymin = -Inf, ymax = Inf, fill = "areas")) +
  geom_segment(aes(x = starts, xend = ends, y = item, yend = item, color = "segments")) +
  scale_color_viridis_d(direction = -1) +
  scale_fill_viridis_d(alpha = 0.5)

Created on 2022-04-12 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文