Treemap

part of a whole
Trees are for climbing

Examples

Garbage

df <- df %>%
  mutate(total_area_numeric = as.numeric(gsub(" \\[m\\^2\\]", "", total_area)))


# Create the treemap using plotly
fig <- plot_ly(
  data = df <- df %>%
  mutate(total_area_numeric = as.numeric(gsub(" \\[m\\^2\\]", "", total_area)))
,
  type = "treemap",
  path = c("domain", "sdsfeature"), # Define the hierarchy for the treemap
  values = ~total_area_numeric,     # Specify the values to determine area size
  textinfo = "label+value+percent parent", # Information to display on each tile
  textfont = list(size = 12),
  hovertemplate = paste(
    '<b>%{label}</b><br>',
    'Area: %{value:.2f} [m^2]<br>',
    'Percent of Parent: %{percentParent:.2%}<extra></extra>'
  ),
  marker = list(colorscale = "Blues") # Choose a color scale (optional)
) %>%
  layout(title = "Treemap of SDS Features by Domain") # Add a title (optional)

# Display the treemap
fig

# Combine the data frames
treemap_data <- dplyr::bind_rows(top_level_data, sub_parents_data, data %>% dplyr::filter(!is.na(status)))

fig <- plotly::plot_ly(
  data = treemap_data,
  ids = ~ids,
  parents = ~parents,
  labels = ~labels,
  values = ~values,
  type = "treemap",
  branchvalues = "total", # How values are aggregated up the tree
  textinfo = "label+value+percent parent+percent entry", # Information to display on each tile
  pathbar = list(visible = TRUE),
  hovertemplate = paste(
    "<b>%{label}</b><br>",
    "Value: %{value}<br>",
    "Status: %{data.status}<br>",
    "Parent: %{parentLabel}<extra></extra>"
  ),
  marker = list(
    colorscale = list(c(0, "lightgray"), c(1, "lightblue")), # Basic color scale
    line = list(color = "white", width = 1)
  )
)

fig <- fig %>%
  plotly::layout(
    title = "Treemap with Status Information",
    treemapcolorway = c("lightgreen", "orange", "lightcoral"), # Example colorway for different branches
    treemapgap = 1,
    treemapgroupgap = 1
  )

fig



stock_data <- data.frame(
  name = sample(LETTERS[1:20], 100, replace = TRUE),
  group = sample(c("Tech", "Finance", "Energy", "Healthcare"), 100, replace = TRUE),
  value = rnorm(100),
  status = factor(sample(c("unknown", "known", "processed"), 100, replace = TRUE),
                  levels = c("unknown", "known", "processed"))
)
stock_data

# You might want to aggregate data within each 'name' if you have multiple rows per stock
aggregated_data <- stock_data %>%
  dplyr::group_by(name, status) %>%
  dplyr::summarise(avg_value = mean(value, na.rm = TRUE)) %>%
  dplyr::ungroup()

ggplot2::ggplot(aggregated_data, ggplot2::aes(x = status, y = name, fill = avg_value)) +
  ggplot2::geom_tile(color = "white") + # Add white borders for better separation
  ggplot2::scale_fill_gradient2(low = "red", mid = "white", high = "green",
                       midpoint = 0, name = "Average Value") + # Customize color gradient
  ggplot2::scale_x_discrete(position = "top") + # Place x-axis labels at the top
  ggplot2::labs(title = "Stock Market Heatmap by Status",
       x = "Status",
       y = "Stock Name") +
  ggplot2::theme_minimal() + # Use a minimal theme for cleaner look
  ggplot2::theme(axis.text.y = ggplot2::element_text(size = 8), # Adjust y-axis text size
        axis.ticks.y = ggplot2::element_blank(), # Remove y-axis ticks
        panel.grid.major = ggplot2::element_blank(), # Remove major grid lines
        panel.border = ggplot2::element_blank(), # Remove panel border
        panel.background = ggplot2::element_blank()) # Remove panel background



ggplot2::ggplot(data, ggplot2::aes(x = 1, y = name, fill = status)) +
  ggplot2::geom_tile(color = "white", linewidth = 1.5) + # Add white borders for better separation
  ggplot2::scale_fill_manual(values = c("unknown" = "red",
                               "known" = "lightgreen",
                               "processed" = "green")) +
  ggplot2::labs(title = "Stock Status Heat Chart",
       x = NULL,
       y = "Stock Name",
       fill = "Status") +
  ggplot2::theme_minimal() +
  ggplot2::theme(axis.text.x = ggplot2::element_blank(), # Remove x-axis labels
        axis.ticks.x = ggplot2::element_blank(), # Remove x-axis ticks
        panel.grid = ggplot2::element_blank())   # Remove grid lines