繁体   English   中英

ggplot2 中跨面的连续调色板

[英]Sequential Colour Palettes Across Facets in ggplot2

我正在构建一个仪表板,其中包括按访问者数量排列的前 10 个位置的棒棒糖图表,按年份分面。 这是我使用虚拟数据构建的大致相似的 plot:

在此处输入图像描述

为了按每个方面的访问者总数重新排序位置, 我使用了由 Tyler Rinker 创建的 reorder_within() 和 scale_x_reorder() 对于顺序调色板,我使用了 RColorBrewer 和 scale_color_distiller。

关于酒吧的 colors,我想更改三件事,但我不确定如何做。

  1. 我希望 colors 从更暗而不是接近白色开始,因为它们有点难以看到。
  2. 我希望每个栏都有自己的颜色,即使访客人数相同,并且这些颜色彼此之间可以清楚地区分,同时仍然是连续的。
  3. 我希望每个方面都有相同的配色方案,所以看起来一致 - 我知道方案在各个方面有所不同,因为它们都有不同的访客人数。

下面带有一些虚拟数据的可重现示例:

library(tidyverse)
library(RColorBrewer)

#facet reorder code - by Tyler Rinker
reorder_within <-
  function(x,
           by,
           within,
           fun = mean,
           sep = "___",
           ...) {
    new_x <- paste(x, within, sep = sep)
    stats::reorder(new_x, by, FUN = fun)
  }

scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(
    labels = function(x)
      gsub(reg, "", x),
    ...
  )
}


dummy <- data.frame(
  Year = rep(c(2019, 2020, 2021), c(10)),
  Destination = c("loc_4322", "loc_43267", "loc_6786", "loc_43294", "loc_45566", 
                  "loc_1234", "loc_367", "loc_14765", "loc_49865", "loc_90765",
                  "loc_4332", "loc_367", "loc_2112", "loc_596111", "loc_54980",
                  "loc_539", "loc_5699", "loc_1965", "loc_6387", "loc_213",
                  "loc_5245", "loc_4787", "loc_34098", "loc_67609", "loc_50954",
                  "loc_54421", "loc_548901", "loc_23245", "loc_4322", "loc_0986"),
  Visitor_numbers = c(102234, 234984, 39546, 108943, 430985, 243056, 342890,
                      253980, 129803, 134954, 21954, 128904, 223242, 223242, 
                      223242, 23242, 243980, 134324, 542323, 12545, 905334,
                      123434, 123434, 569085, 5085, 235463, 209384, 230923,
                      243089, 120923)
)




destinations <- dummy %>%
  ggplot() +
  (aes(
    x = reorder_within(Destination, -Visitor_numbers, Year),
    y = Visitor_numbers,
    color = Visitor_numbers
  )) +
  geom_point(size = 4) +
  geom_segment(
    aes(
      x = reorder_within(Destination, -Visitor_numbers, Year),
      xend = reorder_within(Destination, -Visitor_numbers, Year),
      y = 0,
      yend = Visitor_numbers
    ),
    size = 2
  ) +
  scale_x_reordered() +
  scale_color_distiller(type = "seq", palette = "Oranges", direction = 1) +
  facet_wrap(~ Year,
             dir = "v",
             scales = "free",
             ncol = 1) +
  coord_flip()

destinations

任何帮助将不胜感激。

我建议在一年内按等级或比例值着色。 以下是两种可能:

destinations <- dummy %>%
  group_by(Year) %>%
  mutate(Visitor_numbers_rank = min_rank(Visitor_numbers)) %>%
  #mutate(Visitor_numbers_rank = Visitor_numbers/max(Visitor_numbers)) %>%
  ungroup() %>%
  ggplot(aes(
    x = reorder_within(Destination, -Visitor_numbers, Year),
    y = Visitor_numbers,
    color = Visitor_numbers_rank
  )) +
  guides(color = "none") +
  ...

在此处输入图像描述

要修改调色板以使最亮的值变暗,您可以扩展颜色限制以包含小于任何数据的值,从而有效地将 colors 移动到更远的范围内。 我知道等级 go 不会低于 1,因此色阶从 -5 开始会使一切都发生变化。

  scale_color_distiller(type = "seq", palette = "Oranges",  direction = 1,
                        limits = c(-5, 10)) +

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM