Welcome to nivo.bubblechart. This package brings the power of Nivo’s circle packing visualizations to R, making it straightforward to create interactive bubble charts for data analysis and Shiny applications.
What are bubble charts? Bubble charts (circle packing diagrams) display hierarchical data as nested circles. The size of each bubble represents a quantitative value, making it easy to compare magnitudes across categories.
Create a bubble chart with a few lines of code:
library(nivo.bubblechart)
# Prepare data
fruit_data <- list(
name = "Fruits",
children = list(
list(name = "Apples", value = 450, color = "#ff6b6b", labelColor = "#ffffff"),
list(name = "Bananas", value = 320, color = "#feca57", labelColor = "#000000"),
list(name = "Oranges", value = 280, color = "#ff9f43", labelColor = "#ffffff"),
list(name = "Grapes", value = 150, color = "#a29bfe", labelColor = "#ffffff")
)
)
# Create visualization
bubblechart(
element_id = "fruit_chart",
main_color = "#ff6b6b",
label_color = "#ffffff",
on_hover_title_color = "#2c3e50",
data = fruit_data,
height = "500px"
)The package expects data as a nested list:
hierarchical_data <- list(
name = "root", # Root node name
children = list( # Child nodes
list(
name = "Item 1", # Display name
value = 100, # Bubble size
color = "#3498db", # Fill color
labelColor = "#fff" # Text color
),
list(
name = "Item 2",
value = 200,
color = "#e74c3c",
labelColor = "#fff"
)
)
)| Field | Type | Description |
|---|---|---|
name |
character | Display name for the bubble |
value |
numeric | Determines bubble size (must be positive) |
color |
character | Hex color code for bubble fill |
labelColor |
character | Hex color code for label text |
Note: All bubbles must have the same structure. Missing fields may cause rendering issues.
Most R users work with data frames. The
prepare_bubble_data() function handles the conversion:
# Data frame
sales_data <- data.frame(
product = c("Laptop", "Phone", "Tablet", "Watch", "Headphones"),
revenue = c(45000, 38000, 22000, 15000, 8000),
category_color = c("#3498db", "#3498db", "#3498db", "#e74c3c", "#e74c3c"),
text_color = c("#ffffff", "#ffffff", "#ffffff", "#ffffff", "#ffffff")
)
# Convert to bubble chart format
chart_data <- prepare_bubble_data(
df = sales_data,
name_col = "product",
value_col = "revenue",
color_col = "category_color",
label_color_col = "text_color",
root_name = "Product Sales"
)
# Create chart
bubblechart(
element_id = "sales_chart",
main_color = "#3498db",
label_color = "#ffffff",
on_hover_title_color = "#2c3e50",
data = chart_data,
height = "600px"
)When your data doesn’t include color columns:
Assign colors based on data values:
company_data <- data.frame(
department = c("HR", "IT", "Finance", "Operations", "R&D"),
employees = c(25, 150, 40, 200, 80)
)
# Color by department size
company_data$color <- ifelse(
company_data$employees > 100, "#27ae60",
ifelse(company_data$employees > 50, "#f39c12", "#e74c3c")
)
company_data$labelColor <- "#ffffff"
chart_data <- prepare_bubble_data(company_data)Some tested combinations:
library(shiny)
library(nivo.bubblechart)
sample_data <- list(
name = "Tech Companies",
children = list(
list(name = "Apple", value = 2800, color = "#A3AAAE", labelColor = "#000000"),
list(name = "Microsoft", value = 2500, color = "#00A4EF", labelColor = "#ffffff"),
list(name = "Google", value = 1800, color = "#4285F4", labelColor = "#ffffff"),
list(name = "Amazon", value = 1600, color = "#FF9900", labelColor = "#000000"),
list(name = "Meta", value = 900, color = "#0668E1", labelColor = "#ffffff")
)
)
ui <- fluidPage(
titlePanel("Market Cap Visualization"),
sidebarLayout(
sidebarPanel(
h4("Selected Company"),
verbatimTextOutput("selected_company")
),
mainPanel(
bubblechartOutput("tech_chart", height = "600px")
)
)
)
server <- function(input, output, session) {
output$tech_chart <- renderBubblechart({
bubblechart(
element_id = "tech_bubble",
main_color = "#667eea",
label_color = "#ffffff",
on_hover_title_color = "#FFD700",
activeColor = "#FF6B6B",
borderWidth = 2,
data = sample_data,
height = "600px"
)
})
output$selected_company <- renderPrint({
clicked <- input$tech_bubble_clicked
if (is.null(clicked)) {
cat("Click a bubble to select")
} else if (clicked == "DESELECT_EVENT") {
cat("Deselected")
} else {
cat("Selected:", clicked)
}
})
}
shinyApp(ui, server)server <- function(input, output, session) {
observeEvent(input$my_chart_clicked, {
clicked_item <- input$my_chart_clicked
if (clicked_item == "DESELECT_EVENT") {
showNotification("Deselected", type = "message")
} else {
showNotification(paste("Selected:", clicked_item), type = "message")
}
})
}portfolio <- data.frame(
asset = c("Stocks", "Bonds", "Real Estate", "Cash"),
amount = c(45000, 30000, 20000, 8000)
)
portfolio$color <- c("#e74c3c", "#f39c12", "#3498db", "#27ae60")
portfolio$labelColor <- "#ffffff"
portfolio_data <- prepare_bubble_data(
portfolio,
name_col = "asset",
value_col = "amount"
)web_traffic <- data.frame(
page = c("Home", "Products", "Blog", "About"),
visitors = c(15000, 8500, 6200, 2100),
bounce_rate = c(0.35, 0.42, 0.28, 0.55)
)
# Color by bounce rate
web_traffic$color <- ifelse(
web_traffic$bounce_rate < 0.3, "#27ae60",
ifelse(web_traffic$bounce_rate < 0.5, "#f39c12", "#e74c3c")
)
web_traffic$labelColor <- "#ffffff"
traffic_data <- prepare_bubble_data(
web_traffic,
name_col = "page",
value_col = "visitors"
)Recommendations:
str() before visualizing| Parameter | Type | Default | Description |
|---|---|---|---|
element_id |
character | required | Shiny input ID |
main_color |
character | required | Default bubble color |
label_color |
character | required | Default label color |
on_hover_title_color |
character | required | Hover label color |
data |
list | NULL | Hierarchical data |
width |
character | NULL | Chart width |
height |
character | “400px” | Chart height |
isInteractive |
logical | TRUE | Enable interactions |
activeColor |
character | “transparent” | Selected color |
borderWidth |
numeric | 3 | Border width (px) |
| Parameter | Type | Default | Description |
|---|---|---|---|
df |
data.frame | required | Input data |
name_col |
character | “name” | Name column |
value_col |
character | “value” | Value column |
color_col |
character | “color” | Color column |
label_color_col |
character | “labelColor” | Label color column |
root_name |
character | “root” | Root node name |
default_color |
character | “#ff5f56” | Fallback color |
default_label_color |
character | “#ffffff” | Fallback label color |