Practice Task 1

Load Data

data("diamonds")

Facet Wrap Plot

facet_wrap_plot <- ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
  geom_point(alpha = 0.5) +
  facet_wrap(~cut) +
  labs(title = "Price vs Carat by Cut",
       x = "Carat",
       y = "Price") +
  theme_minimal()

facet_wrap_plot

Facet Grid Plot

facet_grid_plot <- ggplot(diamonds, aes(x = price, fill = cut)) +
  geom_histogram(bins = 30, alpha = 0.7) +
  facet_grid(color ~ cut) +
  labs(title = "Price Distribution by Color and Cut",
       x = "Price",
       y = "Count") +
  theme_minimal()

facet_grid_plot

Practice Task 2

Create Multiple Plots

plot1 <- ggplot(diamonds, aes(carat, price, color = cut)) +
  geom_point(alpha = 0.5) +
  theme_minimal()

plot2 <- ggplot(diamonds, aes(cut, fill = cut)) +
  geom_bar() +
  theme_minimal()

plot3 <- ggplot(diamonds, aes(price, fill = cut)) +
  geom_density(alpha = 0.5) +
  theme_minimal()

plot4 <- ggplot(diamonds, aes(depth, price)) +
  geom_point(alpha = 0.3, color = "blue") +
  theme_minimal()

Combine Plots (Patchwork)

combined_plot <- (plot1 | plot2) / plot3 +
  plot_layout(guides = "collect")

combined_plot

Practice Task 3

Export Files

# Save PNG (300 dpi)
ggsave("combined_plot.png", combined_plot, dpi = 300, width = 10, height = 6)

# Save PDF
ggsave("combined_plot.pdf", combined_plot, width = 10, height = 6)

# Save dataset as CSV
write.csv(diamonds, "diamonds_data.csv", row.names = FALSE)