Each of the 53,940 rows in the ggplot2 diamonds dataset describes one stone. I use all rows throughout the report and keep the original ordering of the cut and color grades.
library(ggplot2)
library(dplyr)
library(scales)
data("diamonds", package = "ggplot2")
knitr::opts_chunk$set(echo = TRUE, fig.width = 8, fig.height = 4.6,
dpi = 120, fig.align = "center", message = FALSE)
theme_set(theme_minimal(base_size = 12) +
theme(panel.grid.minor = element_blank(),
legend.position = "bottom",
plot.title = element_text(face = "bold")))
usd <- label_dollar(accuracy = 1)
pct <- label_percent(accuracy = 0.1)
ggplot(diamonds, aes(x = price)) +
geom_histogram(binwidth = 500, boundary = 0, closed = "left",
fill = "steelblue", colour = "white", linewidth = 0.2) +
scale_x_continuous(labels = usd) +
scale_y_continuous(labels = label_comma()) +
labs(title = "Diamond prices cluster at the lower end",
x = "Price (US dollars)", y = "Number of diamonds")
The histogram counts diamonds in equal $500 intervals, which makes it easy to see where prices cluster. About 51.1% of the stones cost less than $2,500. The distribution is right-skewed, with a long, thinning tail that reaches $18,823.
ggplot(diamonds, aes(x = price, y = cut)) +
geom_boxplot(fill = "#A8D5CF", outlier.alpha = 0.22, outlier.size = 0.8) +
scale_x_continuous(labels = usd) +
labs(title = "Premium has a higher median price than Ideal",
x = "Price (US dollars)", y = "Cut grade")
cut_quartiles <- diamonds |>
group_by(cut) |>
summarise(low = quantile(price, 0.25), middle = median(price),
high = quantile(price, 0.75))
premium <- filter(cut_quartiles, cut == "Premium")
ideal <- filter(cut_quartiles, cut == "Ideal")
The median is $3,185 for Premium and $1,810 for Ideal. Premium also has a wider middle half of prices ($1,046 to $6,296) than Ideal ($878 to $4,678). These boxes compare different groups of stones whose weights and other characteristics can vary, so they cannot isolate the effect of cut on price.
weight_price <- ggplot(diamonds, aes(x = carat, y = price))
weight_price +
geom_point(aes(colour = cut), alpha = 0.23, size = 0.7) +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE,
colour = "#252525", linewidth = 0.85) +
scale_colour_viridis_d(end = 0.85) +
scale_y_continuous(labels = usd) +
guides(colour = guide_legend(override.aes = list(alpha = 1, size = 3))) +
labs(title = "Heavier stones generally sell for more in this sample",
x = "Weight (carats)", y = "Price (US dollars)", colour = "Cut grade")
Around one carat, I can see several cut grades in the same price range. The dark line still slopes upward overall, so heavier diamonds generally cost more in this sample. I put colour = cut inside aes() to give each grade its own point color. I set the line to black outside aes() so the overall trend would be easy to see.
weight_price +
geom_point(colour = "#246967", alpha = 0.18, size = 0.65) +
facet_wrap(~ cut, ncol = 3, scales = "fixed") +
scale_x_continuous(breaks = c(0, 2, 4)) +
scale_y_continuous(labels = usd, breaks = c(0, 5000, 10000, 15000)) +
labs(title = "The upward pattern appears within individual cut grades",
x = "Weight (carats)", y = "Price (US dollars)")
Price generally rises with weight in both the Fair and Ideal panels. The Ideal panel has a dense cluster below one carat, while the Fair panel is much sparser and includes a stone above five carats. Separating the grades makes the small Fair group easier to see than in the combined scatterplot. The shared axes let me compare the same weight and dollar ranges in every panel.
grade_mix <- ggplot(diamonds, aes(x = cut, fill = color))
grade_scale <- scale_fill_viridis_d(option = "C", end = 0.90,
name = "Color grade (D = best)")
grade_mix +
geom_bar(position = "dodge") +
grade_scale +
scale_y_continuous(labels = label_comma()) +
labs(title = "Ideal contributes the most stones of each color grade",
x = "Cut grade", y = "Number of diamonds")
grade_mix +
geom_bar(position = "fill") +
grade_scale +
scale_y_continuous(labels = label_percent()) +
labs(title = "Color composition within each cut grade",
x = "Cut grade", y = "Share of diamonds within cut")
j_share <- diamonds |>
group_by(cut) |>
summarise(share = mean(color == "J"))
The side-by-side bars answer a count question. For example, Ideal includes more J-grade stones than Fair. The 100% bars are better for comparing composition because each segment divides its color-grade count by the total number of diamonds in that cut category. The comparison reverses for J when measured this way: J makes up 7.4% of Fair diamonds and only 4.2% of Ideal diamonds.
price_summary <- diamonds |>
group_by(cut) |>
summarise(n = n(), mean_price = mean(price), median_price = median(price),
.groups = "drop")
price_summary |>
transmute(Cut = cut, Diamonds = comma(n, accuracy = 1),
`Mean price (USD)` = dollar(mean_price, accuracy = 0.01),
`Median price (USD)` = dollar(median_price, accuracy = 0.01)) |>
knitr::kable(align = c("l", "r", "r", "r"),
caption = "Diamond counts and prices by cut grade")
| Cut | Diamonds | Mean price (USD) | Median price (USD) |
|---|---|---|---|
| Fair | 1,610 | $4,358.76 | $3,282.00 |
| Good | 4,906 | $3,928.86 | $3,050.50 |
| Very Good | 12,082 | $3,981.76 | $2,648.00 |
| Premium | 13,791 | $4,584.26 | $3,185.00 |
| Ideal | 21,551 | $3,457.54 | $1,810.00 |
ggplot(price_summary, aes(x = cut, y = mean_price)) +
geom_col(fill = "#387D77", width = 0.68) +
scale_y_continuous(labels = usd) +
labs(title = "Premium has the highest average price in this sample",
x = "Cut grade", y = "Mean price (US dollars)")
ideal_price <- filter(price_summary, cut == "Ideal")
The summary has one row per cut, so the default geom_bar() would count one row per category. I used geom_col() to make the bar height equal to the mean price already calculated in the table. Among Ideal diamonds, the mean of $3,458 exceeds the median of $1,810. The high-priced stones visible in the Ideal boxplot pull the average upward, while the median marks the halfway point in the ordered prices.