Skip to content

Instantly share code, notes, and snippets.

@pachadotdev
Created August 26, 2019 17:26
Show Gist options
  • Save pachadotdev/7cb89e96c67d2dc4f18d7bde12674dbe to your computer and use it in GitHub Desktop.
Save pachadotdev/7cb89e96c67d2dc4f18d7bde12674dbe to your computer and use it in GitHub Desktop.
echarts4r advanced treemap hacks
library(dplyr)
library(stringr)
library(echarts4r)
n <- 5
set.seed(123)
colors <- c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50", "#7f8c8d")
df <- data.frame(x = seq_len(n) - 1) %>%
mutate(
value = 10 + x + 10 * sin(x),
share = value / sum(value),
name = sample(fruit[str_length(fruit) <= 5], size = n),
color = rep(colors, length.out = n),
) %>%
mutate(
value = round(value, 2),
name = paste(name, paste0("\n", round(100 * share,2), "%"))
)
df %>%
e_charts() %>%
e_treemap(name,
name,
value,
breadcrumb = FALSE,
roam = FALSE,
nodeClick = FALSE,
itemStyle = list(
normal = list(
borderWidth = 0,
gapWidth = 1,
backgroundColor = "#d3d3d3"
)
)) %>%
e_title("Fruits",
textStyle = list(fontFamily = "Fira Sans"),
textVerticalAlign = "top",
left = "center") %>%
e_labels(
show = TRUE,
formatter = "{b}\n({@value} B)",
fontFamily = "Fira Sans",
verticalAlign = "top") %>%
e_tooltip(
formatter = e_tooltip_item_formatter(digits = 2)
) %>%
e_toolbox_feature("saveAsImage", title = 'save as image') %>%
e_color(
color = df$color,
background = "#d3d3d3"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment