I have been exploring the use of the "image" option for the symbol parameter in a tree chart with eCharts4r. Despite trying multiple methods, I am struggling to assign a unique image to each node in the tree instead of using a universal one.
However, my attempts have resulted in a strange white overlay covering the rendering of the tree (I can provide images if needed).
Code Chunk 1: Required Libraries
Code Chunk 2: Sample Data
Code Chunk 3: Function Used to Transform Data into Nested Structure
Code Chunk 4: The function in echarts4r for rendering the symbol. Using "image://" as a prefix seems necessary according to documentation. However, most of the time, instead of an image, a light-blue rectangle is rendered.
library(tidyverse)
library(echarts4r)
library(flexdashboard)
tree_data <- data.frame(
name = c("John Jay", "John Rutledge", "William Cushing", "James Wilson", "John Blair", "James Iredell", "Thomas Johnson", "William Paterson", "Samuel Chase", "Oliver Ellsworth", "Bushrod Washington", "Alfred Moore", "John Marshall", "William Johnson", "John McLean", "Levi Woodbery", "William Strong"),
big = c(NA, "John Jay", "John Jay", "William Cushing", "William Cushing", "John Rutledge", "John Rutledge", "John Rutledge", "John Jay", "Samuel Chase", "James Iredell", "William Cushing", "John Blair", "Bushrod Washington", "Bushrod Washington", "John McLean", "William Johnson"),
pledgeClass = c("Founder","A", "A", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "E", "E"),
alumniStatus = c(TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE),
symbolIcon = c("image://https://upload.wikimedia.org/wikipedia/commons/7/72/John_Jay_%28Gilbert_Stuart_portrait%29.jpg",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w",
"image://https://images.squarespace-cdn.com/content/v1/57d1312f29687f39bf5fcd74/50bc8b0c-67f6-4e2d-83f5-e56f255e2a7e/blank_profile_icon.png?format=1500w")
create_nested_structure <- function(data, big_boy) {
data %>%
filter(big %in% big_boy) -> d
if (nrow(d) == 0) {
return(NA)
} else {
return(mutate(d, children = map(name, ~create_nested_structure(data, .x))))
}
}
tree_data_final <- create_nested_structure(tree_data, NA)
tree_data_final %>%
e_charts() %>%
e_tree(initialTreeDepth = -1, #Expands all nodes by default
layout = "orthogonal",
orient = "TB",
symbolSize = 9, #Default is 7
symbol = tree_data_final$symbolIcon,
roam = TRUE) %>% #Enables user interaction like moving and zooming
e_add_nested("tooltip",big) %>%
e_tooltip(trigger = "item",
formatter = htmlwidgets::JS("
function(params){
return('<strong>' + params.name + '</strong>' + '<br />' + 'Pledge Class: ' + params.data.pledgeClass + '<br />' + 'Alumni: ' + params.data.alumniStatus)
}
")