# Example 1: Basic conversion with all columns present
df <- data.frame(
name = c("Apple", "Banana", "Cherry"),
value = c(100, 200, 150),
color = c("#ff5f56", "#ffbd2e", "#27c93f"),
labelColor = c("#ffffff", "#ffffff", "#ffffff")
)
bubble_data <- prepare_bubble_data(df)
str(bubble_data)
# Example 2: Using custom column names
sales_df <- data.frame(
product = c("Laptop", "Phone", "Tablet"),
revenue = c(45000, 38000, 22000),
brand_color = c("#3498db", "#e74c3c", "#f39c12")
)
chart_data <- prepare_bubble_data(
sales_df,
name_col = "product",
value_col = "revenue",
color_col = "brand_color",
default_label_color = "#ffffff"
)
# Example 3: Using default colors (no color columns)
simple_df <- data.frame(
category = c("A", "B", "C", "D"),
amount = c(50, 120, 80, 200)
)
bubble_data <- prepare_bubble_data(
simple_df,
name_col = "category",
value_col = "amount",
default_color = "#9b59b6", # Purple
default_label_color = "#ffffff" # White
)
# Example 4: Conditional coloring based on values
data_df <- data.frame(
item = c("Small", "Medium", "Large", "X-Large"),
size = c(50, 150, 300, 500)
)
# Add colors based on size
data_df$color <- ifelse(data_df$size > 250, "#27ae60", # Green for large
ifelse(data_df$size > 100, "#f39c12", # Orange for medium
"#e74c3c")) # Red for small
data_df$labelColor <- "#ffffff"
bubble_data <- prepare_bubble_data(data_df, name_col = "item", value_col = "size")
# Example 5: Use with bubblechart
if (interactive()) {
library(nivo.bubblechart)
df <- data.frame(
fruit = c("Apples", "Oranges", "Bananas", "Grapes"),
sales = c(450, 280, 320, 150)
)
chart_data <- prepare_bubble_data(
df,
name_col = "fruit",
value_col = "sales",
default_color = "#3498db",
default_label_color = "#ffffff"
)
bubblechart(
element_id = "fruit_sales",
main_color = "#3498db",
label_color = "#ffffff",
on_hover_title_color = "#2c3e50",
data = chart_data,
height = "500px"
)
}
Run the code above in your browser using DataLab