library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.3
library(dplyr)
data("diamonds", package = "ggplot2")
ggplot(diamonds, aes(x=price)) +
geom_histogram(bins = 50, fill = "steelblue") +
labs(
title = "Distribution of Diamond Prices",
x = "Price",
y = "Number of Diamonds"
)
Explanation: Diamond prices are skewed right, with most diamonds concentrated in the lower price ranges. As prices increase, the number of diamonds decreases. A histogram works well here because it helps show distributions and frequency.
ggplot(diamonds, aes(x = price, y = cut)) +
geom_boxplot(fill = "steelblue") +
labs(
title = "Diamond Prices by Cut Quality",
x = "Price",
y = "Cut Quality"
)
Explanation: A box plot shows the difference in diamond price across the categories of cut. Premium diamond have a higher median price and a wider spread than Ideal diamonds. These results show that a better cut doesn’t necessarily mean higher prices. Other factors can influence to price.
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(aes(color = cut), alpha = 0.3) +
geom_smooth(method = "lm", se = FALSE, color = "black") +
labs(
title = "Relationship Between Diamond Weight and Price",
x = "Carat Weight",
y = "Price",
color = "Cut Quality"
)
Explanation: The scatter plot shows a positive relationship between carat weight and price. As carat weight increases, diamond prices generally increase.There’s an overlap between cuts, which means diamonds with similar weights can have different prices and cut qualities.
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(alpha = 0.3, color = "steelblue") +
facet_wrap(~ cut, scales = "fixed") +
labs(
title = "Diamond Weight and Price Across Cut Categories",
x = "Carat Weight",
y = "Price"
)
Explanation: This scatter plot shows a positive relationship between carat weight and price across all cuts. You can see, both Ideal and Premium diamonds generally increase in price as carat weight increases, although Premium diamonds show a slightly wider range of carat weights. Faceting makes it easier to examine each cut category separately without overlapping points from other categories.
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: The side-by-side bar chart shows the number of diamonds in each cut category separated by color grade. Ideal diamonds have the highest overall number of diamonds, while Fair diamonds have the lowest. The chart makes it easier to compare the number of diamonds in each color grade across the different cut categories.
ggplot(diamonds, aes(x = cut, fill = color)) +
geom_bar(position = "fill") +
labs(
title = "Proportion of Diamond Color Grades by Cut",
x = "Cut Quality",
y = "Proportion",
fill = "Color Grade"
)
Explanation: The proportional bar chart shows the percentage of each color grade within each cut category. This chart is better for comparing color-grade composition. Each segment represents the number of diamonds in a specific color grade divided by the total number of diamonds in that cut category. For example, Ideal diamonds have a higher proportion of D-grade diamonds than Premium diamonds.
cut_summary <- diamonds %>%
group_by(cut) %>%
summarise(
count = n(),
mean_price = mean(price),
median_price = median(price)
)
cut_summary
## # A tibble: 5 × 4
## cut count 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(cut_summary, aes(x = cut, y = mean_price)) +
geom_col(fill = "steelblue") +
labs(
title = "Average Diamond Price by Cut Quality",
x = "Cut Quality",
y = "Mean Price"
)
Explanation: I grouped the diamonds by cut category and calculated the count, mean price, and median price for each group. The results show that Premium diamonds have a higher average price than Ideal diamonds. The geom_col() function is appropriate because the mean prices were already calculated, while the default geom_bar() would count observations instead. For Premium diamonds, the mean price is approximately $4,584 compared to a median of $3,185, suggesting that higher-priced diamonds pull the average upward.
ggplot(diamonds, aes(x = color, y = price)) +
geom_boxplot(fill = "steelblue") +
labs(
title = "Diamond Prices Across Color Grades",
x = "Diamond Color Grade",
y = "Price"
)
Explanation: How does color grade affect price? I used a boxplot to compare diamond prices across color grades. The x-axis represents the color grades, while the y-axis represents diamond prices in US dollars. The results show that prices vary across color grades, with some lower-quality color grades having higher median prices than higher-quality grades. This comparison does not account for other characteristics, such as carat weight and cut quality, which may also influence prices.