-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c4311b
commit a34e8ec
Showing
5 changed files
with
1,300 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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)") | ||
|
||
# Adjust layout to prevent overlap | ||
plt.tight_layout(rect=[0, 0, 1, 0.95]) # Adjust rect to make space for the global title | ||
|
||
# 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,12 @@ | ||
import matplotlib.pyplot as plt | ||
import seaborn as sns | ||
|
||
penguins = sns.load_dataset("penguins") | ||
|
||
g = sns.FacetGrid(penguins, row="species", col="sex") | ||
g.map(sns.scatterplot, "bill_length_mm", "bill_depth_mm") | ||
g.fig.suptitle("Penguin Bill Dimensions by Species and Sex", fontsize=16) | ||
g.fig.subplots_adjust(top=0.9) # Adjust subplots to fit the title | ||
g.set_axis_labels("Bill Length (mm)", "Bill Depth (mm)") | ||
g.set_titles(row_template="{row_name}", col_template="{col_name}") | ||
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,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) |
Oops, something went wrong.