How can I add specific colors by levels "Manager" and "Operator" to nodes ? (#3452FF and #FFC900)
I tried
org$Color <- org$Title
levels(org$Color) <- c("#3452FF", "#FFC900")
without success...
org <- data.frame(
Manager = c(
NA, "Ana", "Ana", "Bill", "Bill", "Bill", "Claudette", "Claudette", "Danny",
"Fred", "Fred", "Grace", "Larry", "Larry", "Nicholas", "Nicholas"
),
Employee = c(
"Ana", "Bill", "Larry", "Claudette", "Danny", "Erika", "Fred", "Grace",
"Henri", "Ida", "Joaquin", "Kate", "Mindy", "Nicholas", "Odette", "Peter"
),
Title = c(
"President", "VP Operations", "VP Finance", "Director", "Director", "Scientist",
"Manager", "Manager", "Manager", "Operator", "Operator", "Manager",
"Manager", "Manager", "Operator", "Operator"
)
)
org$Color <- org$Title
levels(org$Color) <- c("#3452FF", "#FFC900")
org$tooltip <- paste0(
org$Employee,
"<br>Title: ",
org$Title,
"<br><img src='https://source.unsplash.com/collection/385548/150x100'>"
)
collapsibleTreeNetwork(
org,
attribute = "Title",
fill = "Color",
nodeSize = "leafCount",
tooltipHtml = "tooltip"
)
According to the docs the fill
parameter is:
either a single color or a column name with the color for each node
This means Color
column needs to have one color per row.
One way to map color to job title to create a factor
(which I think you were getting at with your use of levels()
). I'll use the Set1 palette from RColorBrewer
for this example.
org$Color <- factor(
org$Title,
levels = unique(org$Title),
labels = RColorBrewer::brewer.pal(length(unique(org$Title)), "Set1")
)
This maps one color to each title as follows:
# Color Title
# <fctr> <char>
# 1: #E41A1C President
# 2: #377EB8 VP Operations
# 3: #4DAF4A VP Finance
# 4: #984EA3 Director
# 5: #FF7F00 Scientist
# 6: #FFFF33 Manager
# 7: #A65628 Operator
Then you can draw the plot in the same way:
collapsibleTreeNetwork(
org,
attribute = "Title",
fill = "Color",
nodeSize = "leafCount",
tooltipHtml = "tooltip"
)
Alternatively, it is not necessary to create a factor
, so to just set the Manager and Operator colors and leave all others NA
(which seems to be white by default), you can just assign the relevant color codes to the Color
column.
org <- within(org, {
Color <- NA
Color[Title == "Manager"] <- "#3452FF"
Color[Title == "Operator"] <- "#FFC900"
})
# use the same code to draw the plot