简体   繁体   English

带有来自数据的十六进制填充颜色的 geom_polygon

[英]geom_polygon with hex fill color from data

I have a tibble with the following structure:我有一个具有以下结构的 tibble:

library(tidyverse)
dmd <- tibble(sample = rep(c(1,2,3), each = 2),
              time   = rep(c(1,2),3),
              score  = c(0.69,0.95,1,0.90,1,0.76)) %>%
  mutate(x = map(base::strsplit(paste(sample,
                                      sample + 0.5,
                                      sample,
                                      sample - 0.5,
                                      sep = ","),
                                split = ","),
                 as.numeric)) %>%
  mutate(y = map(base::strsplit(paste(time + 0.5,
                                      time,
                                      time - 0.5,
                                      time,
                                      sep = ","),
                                split = ","),
                 as.numeric)) %>%
  unnest(c(x,y)) %>%
  group_by(sample,time) %>%
  mutate(group_id = cur_group_id()) %>%
  ungroup
# A tibble: 24 × 6
   sample  time score     x     y group_id
    <dbl> <dbl> <dbl> <dbl> <dbl>    <int>
 1      1     1  0.69   1     1.5        1
 2      1     1  0.69   1.5   1          1
 3      1     1  0.69   1     0.5        1
 4      1     1  0.69   0.5   1          1
 5      1     2  0.95   1     2.5        2
 6      1     2  0.95   1.5   2          2
 7      1     2  0.95   1     1.5        2
 8      1     2  0.95   0.5   2          2
 9      2     1  1      2     1.5        3
10      2     1  1      2.5   1          3
# … with 14 more rows

I then use grDevices::rgb to generate a grayscale hex value dcolor based on score .然后我使用grDevices::rgb生成基于score的灰度十六进制值dcolor The if() statement doesn't do much in this example, but it's important that I retain this element for the functionalization of my code. if()语句在这个例子中并没有做太多事情,但重要的是我保留这个元素以实现我的代码的功能化。

minsc <- min(dmd$score)
thrsh <- 0.75
if (minsc < thrsh) {
  floor <- minsc
} else {
  floor <- thrsh
}

dmd <- dmd %>% mutate(temp = (1 - score) / (1 - floor)) %>%
  mutate(dcolor = grDevices::rgb(temp,temp,temp)) %>%
  dplyr::select(-temp)
# A tibble: 24 × 7
   sample  time score     x     y group_id dcolor 
    <dbl> <dbl> <dbl> <dbl> <dbl>    <int> <chr>  
 1      1     1  0.69   1     1.5        1 #FFFFFF
 2      1     1  0.69   1.5   1          1 #FFFFFF
 3      1     1  0.69   1     0.5        1 #FFFFFF
 4      1     1  0.69   0.5   1          1 #FFFFFF
 5      1     2  0.95   1     2.5        2 #292929
 6      1     2  0.95   1.5   2          2 #292929
 7      1     2  0.95   1     1.5        2 #292929
 8      1     2  0.95   0.5   2          2 #292929
 9      2     1  1      2     1.5        3 #000000
10      2     1  1      2.5   1          3 #000000
# … with 14 more rows

If I plot dmd with geom_polygon , dcolor is interpreted as a factor.如果我 plot dmdgeom_polygondcolor被解释为一个因素。

ggplot(dmd) + 
  geom_polygon(mapping = aes(x = x,
                             y = y,
                             group = group_id,
                             fill = dcolor),
               color = "black") +
  theme_void()

钻石,错了

I would like to force ggplot to use the hex values given in dcolor as the fill color for each diamond.我想强制ggplot使用dcolor中给出的十六进制值作为每个菱形的填充颜色。 Using scale_fill_manual gets me closer, but the colors are still incorrect ( #000000 should be black, #FFFFFF should be white).使用scale_fill_manual让我更接近,但 colors 仍然不正确( #000000应该是黑色, #FFFFFF应该是白色)。

pal <- dmd %>% dplyr::select(group_id, dcolor) %>%
  unique %>% dplyr::select(dcolor) %>% as_vector

ggplot(dmd) + 
  geom_polygon(mapping = aes(x = x,
                             y = y,
                             group = group_id,
                             fill = dcolor),
               color = "black") +
  scale_fill_manual(values = palette(pal)) +
  theme_void()

钻石,也错了

You could achieve your desired result using scale_fill_identity :您可以使用scale_fill_identity实现您想要的结果:

library(tidyverse)

ggplot(dmd) + 
  geom_polygon(mapping = aes(x = x,
                             y = y,
                             group = group_id,
                             fill = dcolor),
               color = "black") +
  theme_void() +
  scale_fill_identity(guide = guide_legend())

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

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