Submit only the knitted HTML report. Rename it Assignment4_FirstName_LastName.html. Keep code and results visible. See Assignment_4.pdf for the full questions and point allocations.

library(ggplot2)
library(dplyr)
data("diamonds", package = "ggplot2")

Q1. Price histogram (3 points)

ggplot(diamonds, aes(x = price)) +
  geom_histogram(bins = 30, fill = "steelblue") +
  labs(title = "Distribution of Diamond Prices",
       x = "Price (USD)",
       y = "Number of Diamonds")

Explanation: .The histogram shows that diamond prices are right-skewed, meaning most diamonds are concentrated at lower price levels, while fewer diamonds have very high prices. ## Q2. Price boxplots by cut (3 points)

ggplot(diamonds, aes(x = price, y = cut)) +
  geom_boxplot(fill = "steelblue") +
  labs(title = "Diamond Prices by Cut Quality",
       x = "Price (USD)",
       y = "Cut Quality")

Explanation: The boxplots show that diamond prices vary across the different cut categories. Premium diamonds have a higher median price than Ideal diamonds, while Fair diamonds have a relatively wide price distribution.

Q3. Carat and price, colored by cut (5 points)

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point(aes(color = cut), alpha = 0.4) +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(title = "Relationship Between Diamond Weight and Price",
       x = "Carat",
       y = "Price (USD)",
       color = "Cut Quality")

Explanation: The scatterplot shows a positive relationship between carat weight and price. As the weight of a diamond increases, its price generally increases as well. The different cut categories overlap throughout the graph, showing that diamonds with different cut qualities can have similar weights and prices.

Q4. Carat and price, faceted by cut (4 points)

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point(alpha = 0.3, color = "steelblue") +
  facet_wrap(~ cut) +
  labs(title = "Carat and Price Relationship by Cut",
       x = "Carat",
       y = "Price (USD)")

Explanation: The faceted scatter plots show that the positive relationship between carat weight and price is consistent across the different cut categories.

Q5a. Counts by cut and color (2 points)

ggplot(diamonds, aes(x = cut, fill = color)) +
  geom_bar(position = "dodge") +
  labs(title = "Number of Diamonds by Cut and Color",
       x = "Cut Quality",
       y = "Number of Diamonds",
       fill = "Color Grade")

Explanation: This bar chart compares the number of diamonds across cut categories, with each bar separated by color grade.

Q5b. Proportions by cut and color (1 point)

ggplot(diamonds, aes(x = cut, fill = color)) +
  geom_bar(position = "fill") +
  labs(title = "Proportion of Color Grades Within Each Cut",
       x = "Cut Quality",
       y = "Proportion",
       fill = "Color Grade")

Explanation: This chart shows the proportion of each color grade within each cut category. Each bar represents 100% of the diamonds in that cut category.

Q6. Summary table and mean-price bar chart (6 points)

diamond_s <- diamonds %>%
  group_by(cut) %>%
  summarise(
    number_of_diamonds = n(),
    mean_price = mean(price),
    median_price = median(price)
  )

diamond_s
## # A tibble: 5 × 4
##   cut       number_of_diamonds mean_price median_price
##   <ord>                  <int>      <dbl>        <dbl>
## 1 Fair                    1610      4359.        3282 
## 2 Good                    4906      3929.        3050.
## 3 Very Good              12082      3982.        2648 
## 4 Premium                13791      4584.        3185 
## 5 Ideal                  21551      3458.        1810
ggplot(diamond_s, aes(x = cut, y = mean_price)) +
  geom_col(fill = "steelblue") +
  labs(title = "Average Diamond Price by Cut Quality",
       x = "Cut Quality",
       y = "Mean Price (USD)")

Explanation: This chart shows the proportion of each color grade within each cut category. Each bar represents 100% of the diamonds in that cut category.

Q7. Your own visualization question (4 points)

**How does diamond price vary across different color grades?

ggplot(diamonds, aes(x = color, y = price)) +
  geom_boxplot(fill = "steelblue") +
  labs(title = "Diamond Price by Color Grade",
       x = "Color Grade",
       y = "Price (USD)")

Explanation: This boxplot compares diamond prices across the different color grades.The graph shows that prices vary across color grades, although there is considerable overlap between categories. A boxplot is appropriate because it makes it easy to compare the distribution of prices across multiple groups. One limitation is that the graph does not account for carat weight or cut quality, which can also have a significant impact on diamond prices.

Before submitting, knit to HTML and open the file. Check that all eight plots, the Q6 summary table, code, results, and explanations appear.