-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update api spec to handle multi-plot with multiple layers (#5)
- Loading branch information
1 parent
c930c79
commit 75e103b
Showing
8 changed files
with
2,331 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import seaborn as sns | ||
import matplotlib.pyplot as plt | ||
import pandas as pd | ||
import numpy as np | ||
|
||
# Load example dataset | ||
tips = sns.load_dataset("tips") | ||
|
||
# Prepare data for the bar chart (average total bill by day) | ||
avg_bill = tips.groupby('day')['total_bill'].mean().reset_index() | ||
|
||
# Prepare data for the line chart (average tip by day) | ||
avg_tip = tips.groupby('day')['tip'].mean().reset_index() | ||
|
||
# Start plotting | ||
plt.figure(figsize=(10, 6)) | ||
sns.set(style="whitegrid") | ||
|
||
# Create bar chart | ||
bar = sns.barplot(x='day', y='total_bill', data=avg_bill, color='lightblue', label='Average Total Bill') | ||
|
||
# Create line chart | ||
line = sns.lineplot(x='day', y='tip', data=avg_tip, sort=False, marker='o', color='red', label='Average Tip') | ||
|
||
# Adding legend | ||
plt.legend() | ||
|
||
# Show the plot | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"id": "figure-id", | ||
"library": "seaborn", | ||
"coordinate": "cartesian", | ||
"layout": { | ||
"rows": 1, | ||
"columns": 1 | ||
}, | ||
"title": "Average Bill and Tip by Day of Week", | ||
"panels": [ | ||
{ | ||
"id": "panel-01", | ||
"title": "Bar and Line Chart", | ||
"axes": { | ||
"x": "Day of the Week", | ||
"y": "Amount in USD" | ||
}, | ||
"layers": [ | ||
{ | ||
"type": "bar", | ||
"selector": [], | ||
"data": [ | ||
{ | ||
"x": "Thur", | ||
"y": 17.68 | ||
}, | ||
{ | ||
"x": "Fri", | ||
"y": 17.15 | ||
}, | ||
{ | ||
"x": "Sat", | ||
"y": 20.44 | ||
}, | ||
{ | ||
"x": "Sun", | ||
"y": 21.41 | ||
} | ||
] | ||
}, | ||
{ | ||
"type": "line", | ||
"selector": [], | ||
"data": [ | ||
{ | ||
"x": "Thur", | ||
"y": 2.77 | ||
}, | ||
{ | ||
"x": "Fri", | ||
"y": 2.73 | ||
}, | ||
{ | ||
"x": "Sat", | ||
"y": 2.99 | ||
}, | ||
{ | ||
"x": "Sun", | ||
"y": 3.26 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Load the required libraries | ||
library(ggplot2) | ||
library(palmerpenguins) | ||
library(svglite) | ||
|
||
# Load the penguins dataset | ||
data("penguins") | ||
penguins <- na.omit(penguins) | ||
|
||
# Create a ggplot | ||
p <- ggplot(penguins, aes(x = bill_length_mm, y = bill_depth_mm)) + | ||
geom_point() + | ||
facet_grid(species ~ sex) + | ||
labs( | ||
title = "Penguin Bill Dimensions by Species and Sex", | ||
x = "Bill Length (mm)", | ||
y = "Bill Depth (mm)" | ||
) + | ||
theme( | ||
plot.title = element_text(size = 16, hjust = 0.5), | ||
strip.text = element_text(size = 12), | ||
axis.title = element_text(size = 12) | ||
) | ||
|
||
# Save the plot as an SVG file using ggsave and svglite | ||
ggsave("multi-panel.svg", plot = p, device = svglite, width = 10, height = 8) | ||
|
||
# Extract raw data used for plotting per panel | ||
plot_data <- ggplot_build(p)$data[[1]] | ||
# Split the data by panel | ||
plot_data_split <- split(plot_data, plot_data$PANEL) | ||
|
||
# Save each panel data as a separate data frame object | ||
panel_data_list <- lapply(names(plot_data_split), function(panel) { | ||
panel_data <- plot_data_split[[panel]] | ||
assign(paste0("panel_data_", panel), panel_data, envir = .GlobalEnv) | ||
return(panel_data) | ||
}) | ||
|
||
# Assign names to the list elements for easier reference | ||
names(panel_data_list) <- names(plot_data_split) | ||
|
||
# Load the required library for JSON | ||
library(jsonlite) | ||
|
||
# Save each panel data as a JSON file containing only x and y variables | ||
lapply(names(panel_data_list), function(panel) { | ||
panel_data <- panel_data_list[[panel]][, c("x", "y")] | ||
json_file <- paste0("panel_data_", panel, ".json") | ||
write_json(panel_data, json_file) | ||
}) | ||
|
||
|
||
|
||
# Extract the layout information to get the panel titles | ||
layout_info <- ggplot_build(p)$layout$layout | ||
|
||
# Create a data frame that maps panel numbers to species and sex | ||
panel_mapping <- data.frame( | ||
PANEL = layout_info$PANEL, | ||
species = layout_info$species, | ||
sex = layout_info$sex | ||
) | ||
|
||
print(panel_mapping) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import matplotlib.pyplot as plt | ||
import seaborn as sns | ||
|
||
# Load the penguins dataset using seaborn | ||
penguins = sns.load_dataset("penguins") | ||
|
||
# Get unique species and sexes | ||
species = penguins["species"].dropna().unique() | ||
sexes = penguins["sex"].dropna().unique() | ||
|
||
# Create a figure and a grid of subplots | ||
fig, axes = plt.subplots( | ||
nrows=len(species), ncols=len(sexes), figsize=(12, 8), sharex=True, sharey=True | ||
) | ||
|
||
# Set a global title for the figure | ||
fig.suptitle("Penguin Bill Dimensions by Species and Sex", fontsize=16) | ||
|
||
# Loop through species and sexes to create each subplot | ||
for i, sp in enumerate(species): | ||
for j, sex in enumerate(sexes): | ||
ax = axes[i, j] | ||
subset = penguins[(penguins["species"] == sp) & (penguins["sex"] == sex)] | ||
ax.scatter(subset["bill_length_mm"], subset["bill_depth_mm"]) | ||
|
||
# Set panel-specific titles | ||
ax.set_title(f"{sp} - {sex}", fontsize=12) | ||
|
||
# Set x and y labels for the leftmost and bottom plots only | ||
if i == len(species) - 1: | ||
ax.set_xlabel("Bill Length (mm)") | ||
if j == 0: | ||
ax.set_ylabel("Bill Depth (mm)") | ||
|
||
# Show the plot | ||
plt.show() |
Oops, something went wrong.