Complex Visualization with ggplot2

We can create faceted plots and combine multiple visualizations.

Faceted Plot

rCopy code# Create faceted plots by age group
ggplot(data, aes(x = height, y = weight)) +
  geom_point(aes(color = age_group), alpha = 0.7) +
  facet_wrap(~ age_group) +
  labs(title = "Height vs Weight by Age Group",
       x = "Height (cm)",
       y = "Weight (kg)") +
  theme_minimal()

Combining Plots

You can also use the patchwork package to combine multiple plots:

rCopy code# Install and load patchwork
install.packages("patchwork")
library(patchwork)

# Scatter plot and boxplot
scatter_plot <- ggplot(data, aes(x = height, y = weight)) + geom_point() + theme_minimal()
box_plot <- ggplot(data, aes(x = age_group, y = weight)) + geom_boxplot() + theme_minimal()

# Combine plots
combined_plot <- scatter_plot + box_plot + plot_layout(ncol = 2)
print(combined_plot)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *