Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent RColorBrewer::brewer.pal warning #1999

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prevent RColorBrewer::brewer.pal warning
`RColorBrewer::brewer.pal()` throws an (in this case) unnecessary warning if the output plot requires less than 2 colors. This is an easy fix, and does not change the workings of the code since `RColorBrewer::brewer.pal()` never returns less than three levels anywas:

```
> RColorBrewer::brewer.pal(2, "Set2")
[1] "#66C2A5" "#FC8D62" "#8DA0CB"
Warning message:
In RColorBrewer::brewer.pal(2, "Set2") :
  minimal value for n is 3, returning requested palette with 3 different levels
```
s-fleck authored Sep 14, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 7703cdd7ff199454407856d6aa893e1a4e1dfb10
2 changes: 1 addition & 1 deletion R/plotly_build.R
Original file line number Diff line number Diff line change
@@ -785,7 +785,7 @@ map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "
isOrdered <- all(vapply(color[isDiscrete], is.ordered, logical(1)))
lvls <- getLevels(unlist(color[isDiscrete]))
N <- length(lvls)
pal <- palette %||% if (isOrdered) viridisLite::viridis(N) else RColorBrewer::brewer.pal(N, "Set2")
pal <- palette %||% if (isOrdered) viridisLite::viridis(N) else RColorBrewer::brewer.pal(max(N, 3L), "Set2")
colScale <- scales::col_factor(pal, levels = names(pal) %||% lvls, na.color = na.color)
color_codes <- Map(function(x, y) toRGB(colScale(as.character(x)), y), color[isDiscrete], alphas[isDiscrete])
traces[isDiscrete] <- Map(mapColor, traces[isDiscrete], color_codes)