-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathcolor-mapping.R
110 lines (100 loc) · 2.03 KB
/
color-mapping.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
library(plotly)
library(purrr)
p1 <- plotly_empty() %>%
add_annotations(
text = "<b>Data space</b>",
x = 0,
xshift = 20,
y = 0.5,
xref = "paper",
yref = "paper",
xanchor = "left",
showarrow = FALSE
) %>%
add_annotations(
text = "<b>Visual space</b>",
x = 1,
y = 0.5,
xref = "paper",
yref = "paper",
xanchor = "right",
showarrow = FALSE
)
lvls <- levels(diamonds$clarity)
lvls <- factor(lvls, lvls)
cols <- scales::col_factor("Accent", domain = NULL)(lvls)
n <- length(lvls)
grid <- c(0, cumsum(rep(1/n, n)))
y <- map2_dbl(grid[-length(grid)], grid[-1], ~(.x+.y)/2)
rects <- map(
cols,
~list(
type = "rect",
fillcolor = .x,
x0 = 0.75,
x1 = 0.85,
xref = "paper",
yref = "paper",
line = list(width = 1)
)
)
for (i in seq_len(n)) {
rects[[i]]$y0 <- grid[i]
rects[[i]]$y1 <- grid[i+1]
}
p2 <- plotly_empty() %>%
add_annotations(
text = paste(lvls, " "),
x = 0.70,
y = y,
xref = "paper",
yref = "paper",
xanchor = "right",
yanchor = "middle",
ax = -100,
ay = 0
) %>%
add_annotations(
text = cols,
x = 0.85,
y = y,
xref = "paper",
yref = "paper",
xanchor = "left",
yanchor = "middle",
showarrow = FALSE,
font = list(size = 8)
) %>%
layout(
shapes = rects
)
p3 <- plotly_empty() %>%
add_annotations(
text = "<b>Diamond<br>clarity</b>",
x = 0,
xshift = 20,
y = 0,
xref = "paper",
yref = "paper",
xanchor = "left",
yanchor = "bottom",
showarrow = FALSE
) %>%
add_annotations(
text = "<b>RColorBrewer<br>Accent palette</b>",
x = 1,
y = 0,
xref = "paper",
yref = "paper",
xanchor = "right",
yanchor = "bottom",
showarrow = FALSE
)
p <- subplot(p1, p2, p3, nrows = 3, heights = c(0.1, 0.8, 0.1)) %>%
layout(
height = 450,
width = 290,
margin = list(l = 0, b = 0, t = 0, r = 0)
)
withr::with_dir("images", orca(p, "color-mapping.svg"))
withr::with_dir("images", orca(p, "color-mapping.pdf"))