Skip to content

Commit 72b588b

Browse files
committed
fix: update color handling in various plot functions to ensure consistent NA value representation
1 parent e8350a1 commit 72b588b

10 files changed

Lines changed: 52 additions & 41 deletions

File tree

R/areaplot.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ AreaPlotAtomic <- function(
9797
# }
9898
group_by_vals <- levels(data[[group_by]])
9999
if (anyNA(data[[group_by]])) group_by_vals <- c(group_by_vals, NA)
100+
group_colors <- palette_this(group_by_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
100101

101102
just <- calc_just(x_text_angle)
102103

@@ -124,14 +125,14 @@ AreaPlotAtomic <- function(
124125
p <- p +
125126
scale_fill_manual(
126127
name = group_name %||% group_by,
127-
values = palette_this(group_by_vals, palette = palette, palcolor = palcolor),
128+
values = group_colors, na.value = group_colors["NA"] %||% "grey80",
128129
breaks = group_by_vals, limits = group_by_vals, drop = FALSE
129130
)
130131
} else {
131132
p <- p +
132133
scale_fill_manual(
133134
name = group_name %||% group_by,
134-
values = palette_this(group_by_vals, palette = palette, palcolor = palcolor)
135+
values = group_colors, na.value = group_colors["NA"] %||% "grey80"
135136
)
136137
}
137138

R/barplot.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ BarPlotSingle <- function(
146146
)
147147
}
148148
p <- p +
149-
# scale_fill_manual(name = x, values = colors, guide = guide) +
150149
labs(title = title, subtitle = subtitle, x = xlab %||% x, y = ylab %||% y) +
151150
scale_x_discrete(expand = expand$x, drop = !isTRUE(keep_empty_x)) +
152151
scale_y_continuous(expand = expand$y) +
@@ -321,7 +320,7 @@ BarPlotGrouped <- function(
321320
p <- p + bg_layer(data, x, isTRUE(keep_empty_x), bg_palette, bg_palcolor, bg_alpha, facet_by)
322321
}
323322

324-
colors <- palette_this(group_vals, palette = palette, palcolor = palcolor)
323+
colors <- palette_this(group_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
325324
just <- calc_just(x_text_angle)
326325
if (position == "auto") {
327326
position <- if (length(colors) <= 5) {
@@ -370,15 +369,15 @@ BarPlotGrouped <- function(
370369
p <- p +
371370
scale_fill_manual(
372371
name = group_name %||% group_by,
373-
values = colors,
372+
values = colors, na.value = colors["NA"] %||% "grey80",
374373
guide = guide_legend(order = 1),
375374
breaks = group_vals, limits = group_vals, drop = FALSE
376375
)
377376
} else {
378377
p <- p +
379378
scale_fill_manual(
380379
name = group_name %||% group_by,
381-
values = colors,
380+
values = colors, na.value = colors["NA"] %||% "grey80",
382381
guide = guide_legend(order = 1)
383382
)
384383
}
@@ -941,19 +940,21 @@ SplitBarPlotAtomic <- function(
941940
} else {
942941
c(levels(data[[fill_by]]), NA)
943942
}
943+
fill_colors <- palette_this(fill_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
944+
944945
if (isTRUE(keep_empty_fill)) {
945946
p <- p +
946947
scale_fill_manual(
947948
name = fill_name %||% fill_by,
948-
values = palette_this(fill_vals, palette = palette, palcolor = palcolor),
949+
values = fill_colors, na.value = fill_colors['NA'] %||% "grey80",
949950
guide = guide_legend(order = 1),
950951
breaks = fill_vals, limits = fill_vals, drop = FALSE
951952
)
952953
} else {
953954
p <- p +
954955
scale_fill_manual(
955956
name = fill_name %||% fill_by,
956-
values = palette_this(fill_vals, palette = palette, palcolor = palcolor),
957+
values = fill_colors, na.value = fill_colors['NA'] %||% "grey80",
957958
guide = guide_legend(order = 1)
958959
)
959960
}

R/boxviolinplot.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,31 +378,35 @@ BoxViolinPlotAtomic <- function(
378378
if (fill_mode == "dodge") {
379379
group_vals <- levels(data[[group_by]])
380380
if (anyNA(group_vals)) group_vals <- c(group_vals, NA)
381+
group_colors <- palette_this(group_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
382+
381383
if (isTRUE(keep_empty_group)) {
382384
p <- p + scale_fill_manual(
383385
name = group_name %||% group_by,
384-
values = palette_this(group_vals, palette = palette, palcolor = palcolor),
386+
values = group_colors, na.value = group_colors['NA'] %||% "grey80",
385387
breaks = group_vals, limits = group_vals, drop = FALSE
386388
)
387389
} else {
388390
p <- p + scale_fill_manual(
389391
name = group_name %||% group_by,
390-
values = palette_this(group_vals, palette = palette, palcolor = palcolor)
392+
values = group_colors, na.value = group_colors['NA'] %||% "grey80"
391393
)
392394
}
393395
} else if (fill_mode == "x") {
394396
x_vals <- levels(data[[x]])
395397
if (anyNA(x_vals)) x_vals <- c(x_vals, NA)
398+
x_colors <- palette_this(x_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
399+
396400
if (isTRUE(keep_empty_x)) {
397401
p <- p + scale_fill_manual(
398402
name = x,
399-
values = palette_this(x_vals, palette = palette, palcolor = palcolor),
403+
values = x_colors, na.value = x_colors['NA'] %||% "grey80",
400404
breaks = x_vals, limits = x_vals, drop = FALSE
401405
)
402406
} else {
403407
p <- p + scale_fill_manual(
404408
name = x,
405-
values = palette_this(x_vals, palette = palette, palcolor = palcolor)
409+
values = x_colors, na.value = x_colors['NA'] %||% "grey80"
406410
)
407411
}
408412
} else {
@@ -853,14 +857,16 @@ BoxViolinPlotAtomic <- function(
853857
if (!is.null(group_by)) {
854858
group_vals <- levels(data[[group_by]])
855859
if (anyNA(group_vals)) group_vals <- c(group_vals, NA)
860+
group_colors <- palette_this(group_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
861+
856862
if (isTRUE(keep_empty_group)) {
857863
p <- p + scale_color_manual(
858-
values = palette_this(group_vals, palette = palette, palcolor = palcolor),
864+
values = group_colors, na.value = group_colors['NA'] %||% "grey80",
859865
breaks = group_vals, limits = group_vals, drop = FALSE
860866
)
861867
} else {
862868
p <- p + scale_color_manual(
863-
values = palette_this(group_vals, palette = palette, palcolor = palcolor)
869+
values = group_colors, na.value = group_colors['NA'] %||% "grey80"
864870
)
865871
}
866872
}

R/densityplot.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,26 @@ DensityHistoPlotAtomic <- function(
8989
}
9090
group_vals <- levels(data[[group_by]])
9191
if (anyNA(data[[group_by]])) group_vals <- c(group_vals, NA)
92+
group_colors <- palette_this(group_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
9293

9394
p <- ggplot(data, aes(x = !!sym(x), fill = !!sym(group_by), color = !!sym(group_by)))
9495
if (isTRUE(keep_empty_group)) {
9596
p <- p + scale_fill_manual(
9697
name = group_name %||% group_by,
97-
values = palette_this(group_vals, palette = palette, palcolor = palcolor),
98+
values = group_colors, na.value = group_colors['NA'] %||% "grey80",
9899
breaks = group_vals, limits = group_vals, drop = FALSE
99100
) + scale_color_manual(
100101
name = group_name %||% group_by,
101-
values = palette_this(group_vals, palette = palette, palcolor = palcolor),
102+
values = group_colors, na.value = group_colors['NA'] %||% "grey80",
102103
breaks = group_vals, limits = group_vals, drop = FALSE
103104
)
104105
} else {
105106
p <- p + scale_fill_manual(
106107
name = group_name %||% group_by,
107-
values = palette_this(group_vals, palette = palette, palcolor = palcolor)
108+
values = group_colors, na.value = group_colors['NA'] %||% "grey80"
108109
) + scale_color_manual(
109110
name = group_name %||% group_by,
110-
values = palette_this(group_vals, palette = palette, palcolor = palcolor)
111+
values = group_colors, na.value = group_colors['NA'] %||% "grey80"
111112
)
112113
}
113114

R/jitterplot.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,23 +285,23 @@ JitterPlotAtomic <- function(
285285
if (has_fill) {
286286
if (isTRUE(keep_empty_col)) {
287287
p <- p + scale_fill_manual(
288-
name = color_col, values = colors,
288+
name = color_col, values = colors, na.value = colors['NA'] %||% "grey80",
289289
breaks = col_levels, limits = col_levels, drop = FALSE
290290
)
291291
} else {
292-
p <- p + scale_fill_manual(name = color_col, values = colors)
292+
p <- p + scale_fill_manual(name = color_col, values = colors, na.value = colors['NA'] %||% "grey80")
293293
}
294294
if (isTRUE(border)) {
295-
p <- p + scale_color_manual(values = colors, guide = "none")
295+
p <- p + scale_color_manual(values = colors, guide = "none", na.value = colors['NA'] %||% "grey80")
296296
}
297297
} else {
298298
if (isTRUE(keep_empty_col)) {
299299
p <- p + scale_color_manual(
300-
name = color_col, values = colors,
300+
name = color_col, values = colors, na.value = colors['NA'] %||% "grey80",
301301
breaks = col_levels, limits = col_levels, drop = FALSE
302302
)
303303
} else {
304-
p <- p + scale_color_manual(name = color_col, values = colors)
304+
p <- p + scale_color_manual(name = color_col, values = colors, na.value = colors['NA'] %||% "grey80")
305305
}
306306
}
307307

R/lineplot.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ LinePlotSingle <- function(
121121
x_vals <- levels(data[[x]])
122122
if (anyNA(data[[x]])) x_vals <- c(x_vals, NA)
123123
colors <- palette_this(x_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
124-
na_value <- ifelse(anyNA(x_vals), colors["NA"], "grey50")
125124

126125
if (isTRUE(color_line_by_x)) {
127126
p <- p + geom_line(
@@ -132,12 +131,12 @@ LinePlotSingle <- function(
132131
p <- p + scale_color_manual(
133132
name = x, values = colors, guide = "legend",
134133
breaks = x_vals, limits = x_vals, drop = FALSE,
135-
na.value = na_value
134+
na.value = colors["NA"] %||% "grey80"
136135
)
137136
} else {
138137
p <- p + scale_color_manual(
139138
name = x, values = colors, guide = "legend",
140-
na.value = na_value
139+
na.value = colors["NA"] %||% "grey80"
141140
)
142141
}
143142
} else {
@@ -170,12 +169,12 @@ LinePlotSingle <- function(
170169
p <- p + scale_fill_manual(
171170
name = x, values = colors, guide = "legend",
172171
breaks = x_vals, limits = x_vals, drop = FALSE,
173-
na.value = na_value
172+
na.value = colors["NA"] %||% "grey80"
174173
)
175174
} else {
176175
p <- p + scale_fill_manual(
177176
name = x, values = colors, guide = "legend",,
178-
na.value = na_value
177+
na.value = colors["NA"] %||% "grey80"
179178
)
180179
}
181180
} else {
@@ -308,7 +307,7 @@ LinePlotGrouped <- function(
308307
if (anyNA(data[[group_by]])) group_vals <- c(group_vals, NA)
309308

310309
colors <- palette_this(group_vals, palette = palette, palcolor = palcolor, NA_keep = TRUE)
311-
na_value <- ifelse(anyNA(group_vals), colors["NA"], "grey50")
310+
312311
if (!is.null(add_hline) && !isFALSE(add_hline)) {
313312
if (isTRUE(hline_color)) {
314313
if (!is.list(add_hline)) {
@@ -331,11 +330,14 @@ LinePlotGrouped <- function(
331330

332331
if (isTRUE(keep_empty_group)) {
333332
p <- p + scale_color_manual(
334-
name = group_by, values = colors, guide = "legend",
333+
name = group_by, values = colors, guide = "legend", na.value = colors["NA"] %||% "grey80",
335334
breaks = group_vals, limits = group_vals, drop = FALSE
336335
)
337336
} else {
338-
p <- p + scale_color_manual(name = group_by, values = colors, guide = "legend")
337+
p <- p + scale_color_manual(
338+
name = group_by, values = colors, guide = "legend",
339+
na.value = colors["NA"] %||% "grey80"
340+
)
339341
}
340342

341343
if (isTRUE(add_errorbars)) {
@@ -357,12 +359,12 @@ LinePlotGrouped <- function(
357359
p <- p + scale_fill_manual(
358360
name = group_by, values = colors, guide = "legend",
359361
breaks = group_vals, limits = group_vals, drop = FALSE,
360-
na.value = na_value
362+
na.value = colors["NA"] %||% "grey80"
361363
)
362364
} else {
363365
p <- p + scale_fill_manual(
364366
name = group_by, values = colors, guide = "legend",
365-
na.value = na_value
367+
na.value = colors["NA"] %||% "grey80"
366368
)
367369
}
368370

R/piechart.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ PieChartAtomic <- function(
110110

111111
if (isTRUE(keep_empty_x)) {
112112
p <- p + scale_fill_manual(
113-
name = x, values = colors,
113+
name = x, values = colors, na.value = colors["NA"] %||% "grey80",
114114
breaks = rev(x_vals), limits = rev(x_vals), drop = FALSE,
115115
guide = guide_legend(reverse = clockwise)
116116
)
117117
} else {
118118
p <- p + scale_fill_manual(
119-
name = x, values = colors,
119+
name = x, values = colors, na.value = colors["NA"] %||% "grey80",
120120
breaks = rev(x_vals),
121121
guide = guide_legend(reverse = clockwise)
122122
)

R/radarplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ RadarPlotAtomic <- function(
273273

274274
if (isTRUE(keep_empty_group)) {
275275
p <- p + scale_fill_manual(
276-
name = group_name %||% group_by, values = group_colors,
276+
name = group_name %||% group_by, values = group_colors, na.value = group_colors["NA"] %||% "grey80",
277277
breaks = group_vals, limits = group_vals, drop = FALSE
278278
) + scale_color_manual(
279-
values = group_colors,
279+
values = group_colors, na.value = group_colors["NA"] %||% "grey80",
280280
breaks = group_vals, limits = group_vals, drop = FALSE
281281
)
282282
} else {

R/ringplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ RingPlotAtomic <- function(
120120
if (isTRUE(keep_empty_group)) {
121121
p <- p + scale_fill_manual(
122122
name = group_name %||% group_by,
123-
values = colors,
123+
values = colors, na.value = colors["NA"] %||% "grey80",
124124
breaks = rev(group_vals),
125125
limits = rev(group_vals),
126126
drop = FALSE,
@@ -129,7 +129,7 @@ RingPlotAtomic <- function(
129129
} else {
130130
p <- p + scale_fill_manual(
131131
name = group_name %||% group_by,
132-
values = colors,
132+
values = colors, na.value = colors["NA"] %||% "grey80",
133133
breaks = rev(group_vals),
134134
drop = TRUE,
135135
guide = group_guide

R/velocityplot.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ VelocityPlot <- function(
249249
if (isTRUE(keep_empty)) {
250250
velocity_layer[[length(velocity_layer) + 1]] <- scale_color_manual(
251251
name = group_name,
252-
values = group_cols,
252+
values = group_cols, na.value = group_cols["NA"] %||% "grey80",
253253
breaks = group_vals,
254254
limits = group_vals,
255255
drop = FALSE,
@@ -258,7 +258,7 @@ VelocityPlot <- function(
258258
} else {
259259
velocity_layer[[length(velocity_layer) + 1]] <- scale_color_manual(
260260
name = group_name,
261-
values = group_cols,
261+
values = group_cols, na.value = group_cols["NA"] %||% "grey80",
262262
guide = guide_legend(title.hjust = 0, order = 1, override.aes = list(linewidth = 2, alpha = 1))
263263
)
264264
}

0 commit comments

Comments
 (0)