Using either penguins or diamonds:
Create a faceted plot: Example: distribution or scatterplot Create one using facet_wrap, and one using facet_grid in the layout that makes the most sense Make sure: Labels are clear Colors are readable Theme is clean
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(diamonds)
library(ggplot2)
#facet wrap chart for distribution using the diamonds dataset
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_histogram(bins = 5, color = "white", alpha = 0.8) +
facet_wrap(~ cut) +
labs(
title = "Distribution of Diamond Prices by Cut",
x = "Price (USD)",
y = "Count",
fill = "Cut Quality"
) +
theme_classic() +
theme(axis.text.x = element_text(angle = 30, hjust = 1))
#facet grid chart for distribution using the diamonds dataset
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_histogram(bins = 5, color = "white", alpha = 0.8) +
facet_grid(~ cut) +
labs(
title = "Distribution of Diamond Prices by Cut",
x = "Price (USD)",
y = "Count",
fill = "Cut Quality"
) +
theme_classic() +
theme(axis.text.x = element_text(angle = 30, hjust = 1))
Create 3–4 different plots (e.g., scatterplot, barplot, density plot) Combine them into a single multi-panel figure using patchwork/ggforce Make sure you collect the legends when appropriate.
p1 = ggplot(mpg, aes(displ, hwy )) +
geom_point(color = "purple") +
labs(
title = "Engine Size vs Highway MPG",
x = "Engine Displacement",
y = "Highway MPG"
) +
theme_classic()
p2 = ggplot(mpg, aes(hwy,fill= class )) +
geom_histogram(bins = 10, position = "dodge") +
labs(
title = "Distribution of Highway MPG by Class",
x = "Highway MPG",
y = "Count",
fill = "Vehicle Class") +
theme_classic() +
scale_fill_brewer(palette = "Set2")
p3 = ggplot(mpg, aes(class,hwy, fill = class)) +
geom_violin(trim = FALSE, alpha = 0.7, color = "white") +
geom_boxplot(width = 0.15, fill = "white", outlier.size = 0.8) +
labs(
title = "Highway MPG Distribution by Vehicle Class",
x = "Vehicle Class",
y = "Highway MPG",
fill = "Class"
) +
scale_fill_brewer(palette = "Set2") +
theme_classic()
install.packages("gridExtra") # run once
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
combined_figure = grid.arrange(p1, p2, p3, ncol = 2)
Try: Side-by-side layout Stacked layout Unequal sizes
Please write code to perform each of these functions Export your combined figure:
As a PNG (300 dpi) As a PDF Export your dataframe as a CSV file, and your PNG as an image.
# Save PNG
ggsave(
"combined_figure.png",
plot = grid.arrange(p1, p2, p3, ncol = 2),
dpi = 300
)
## Saving 7 x 5 in image
# Save PDF
ggsave(
"combined_figure.pdf",
plot = grid.arrange(p1, p2, p3, ncol = 2),
)
## Saving 7 x 5 in image
ggsave("combined_figure.png", combined_figure, dpi = 300)
## Saving 7 x 5 in image
ggsave("combined_figure.pdf", combined_figure)
## Saving 7 x 5 in image
write.csv(mpg, "mpg.csv", row.names = FALSE)