-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette-tester.R
204 lines (178 loc) · 5.1 KB
/
palette-tester.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# ---- Palette Tester ----
#
# Author: Francesco Grassi
# GitHub: Fra-Gra
#
# A little shiny app to test color palettes.
# The app allows users to input colors in hex format and have a quick visualization of how the colors
# appear in different plot types.
library(shiny)
library(ggplot2)
library(dplyr)
library(stringr)
library(RColorBrewer)
# Define UI
ui <- fluidPage(
titlePanel("Color Palette Tester"),
hr(), # horizontal rule
# Row with three plots:
fluidRow(
# Tabs to switch from manual to Coolors url input
column(
width = 3,
tabsetPanel(id = "input_source",
tabPanel(
"Manual",
textInput("color1", "Color 1 (hex):", value = ""),
textInput("color2", "Color 2 (hex):", value = ""),
textInput("color3", "Color 3 (hex):", value = ""),
textInput("color4", "Color 4 (hex):", value = ""),
textInput("color5", "Color 5 (hex):", value = "")
),
tabPanel(
"Coolors.co",
textInput(
"colorUrl",
"Coolor.co palette url:",
value = "https://coolors.co/palette/264653-2a9d8f-e9c46a-f4a261-e76f51"
)
)
)
),
# Bar plot next to input
column(
width = 4,
offset = 1,
plotOutput("barplot")
),
# Scatter plot next to bar plot
column(
width = 4,
plotOutput("scatterplot")
)
),
fluidRow(
# Line plot under bar plot
column(
width = 4,
offset = 4,
plotOutput("lineplot")
),
# Density plot under scatter plot
column(
width = 4,
plotOutput("densityplot")
)
)
)
# Define server
server <- function(input, output) {
# Deal with color input --------
# Reactive expression to define colors based on input:
color_palette <- reactive({
# If "Manual" tab is selected, check how many colors are provided:
if (input$input_source == "Manual"){
# If no color is provided, assign 5 default colors:
if(!isTruthy(c(input$color1, input$color2, input$color3, input$color4))){
color_palette <- brewer.pal(n = 5, name = "Dark2")
} else {
# Otherwise use only valid color inputs:
color_palette <- c(input$color1, input$color2, input$color3, input$color4)
color_palette <- color_palette[color_palette != ""]
}
} else{
# If "Coolors" tab is selected, extract colors from url:
color_palette <- paste0(
"#", # add "#"...
str_split(
str_remove(input$colorUrl, "https://coolors.co/palette/"), # ... after removing unused url bit...
"-" # ... and splitting the rest into a vector
)[[1]]
)
}
})
# Reactive expression to get length of defined palette:
palette_length <- reactive({
length(color_palette())
})
# Create data.frames for plots --------
# Bar plot:
df_bar <- reactive({
data.frame(
x = 1:palette_length(),
y = sample(1:5, palette_length())
)
})
# Scatter plot:
df_scatter <- reactive({
data.frame(
x = rep(1:palette_length(), each = 10) + rnorm(10*palette_length()),
y = rep(1:palette_length(), each = 10) + rnorm(10*palette_length()),
col = factor(rep(1:palette_length(), each = 10))
)
})
# Line plot:
df_line <- reactive({
data.frame(
x = rep(1:10, times = palette_length()),
y = runif(10 * palette_length()),
col = factor(rep(1:palette_length(), each = 10))
)
})
# Density plot:
df_density <- reactive({
data.frame(
x = rnorm(1000 * palette_length(), 1:palette_length(), 1:palette_length()),
col = factor(rep(1:palette_length(), times = 1000))
)
})
# Functions to create plots --------
# Bar plot:
output$barplot <- renderPlot({
ggplot(df_bar(), aes(x, y)) +
geom_bar(stat = "identity", fill = color_palette()) +
ggtitle("Bar Plot") +
theme_void() +
theme(
plot.title = element_text(size = 30, hjust = 0.5)
)
})
# Scatter plot:
output$scatterplot <- renderPlot({
ggplot(df_scatter(), aes(x, y, color = col)) +
geom_point(size = 3) +
scale_color_manual(values = color_palette()) +
ggtitle("Scatter Plot") +
theme_void() +
theme(
plot.title = element_text(size = 30, hjust = 0.5),
legend.position = "none"
)
})
# Line plot:
output$lineplot <- renderPlot({
ggplot(df_line(), aes(x, y, color = col)) +
geom_line(linewidth = 2) +
scale_color_manual(values = color_palette()) +
ggtitle("Line Plot") +
theme_void() +
theme(
plot.title = element_text(size = 30, hjust = 0.5),
legend.position = "none"
)
})
# Density plot:
output$densityplot <- renderPlot({
ggplot(df_density(), aes(x, fill = col)) +
geom_density(alpha = .40, color = NA) +
scale_fill_manual(values = color_palette()) +
ggtitle("Density Plot") +
theme_void() +
theme(
plot.title = element_text(size = 30, hjust = 0.5),
legend.position = "none"
)
})
}
# Run app
shinyApp(ui, server)