Part 1: Facet_wrap

install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(ggplot2)


ggplot(diamonds, aes(x = price, fill = cut)) +
  geom_histogram(bins = 20, color = "red", alpha = 0.6) +
  facet_wrap(~ cut) +
  labs(
    title = "Distribution of Diamond Prices by Cut",
    x = "Price (USD)",
    y = "Count"
  ) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal()

Facet_grid

ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
  geom_point(alpha = 0.7) +
  facet_grid(cut ~ color) +
  labs(
    title = "Diamond Price vs Carat by Cut and Color",
    x = "Carat",
    y = "Price (USD)",
    color = "Cut Quality"
  ) +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal()

Part 2:

library(ggplot2)
install.packages("patchwork")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(patchwork)

Scatterplot

#scatterplot
p1 <- ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
  geom_point(alpha = 0.6) +
  labs(title = "Carat vs Price") +
  theme_minimal()
#barplot

p2 <- ggplot(diamonds, aes(x = cut, fill = cut)) +
  geom_bar() +
  labs(title = "Count of Diamonds by Cut", x = "Cut", y = "Count") +
  theme_minimal()

#density plot

p3 <- ggplot(diamonds, aes(x = price, fill = cut)) +
  geom_density(alpha = 0.5) +
  labs(title = "Price Distribution by Cut") +
  theme_minimal()

#boxplot
p4 <- ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
  geom_boxplot() +
  labs(title = "Price by Cut") +
  theme_minimal()

#combining the plots

# Combine plots
combined_plot <- (p1 | p2) / (p3 | p4) +
  plot_layout(guides = "collect") & 
  theme(legend.position = "bottom")

combined_plot

#PNG (300 dpi)

ggsave(
  filename = "combined_figure.png",
  plot = combined_plot,
  width = 10,
  height = 8,
  dpi = 300
)

#PDF

ggsave(
  filename = "combined_figure.pdf",
  plot = combined_plot,
  width = 10,
  height = 8
)


write.csv(diamonds, "diamonds_data.csv", row.names = FALSE)