library(ggplot2)
library(dplyr)
library(corrplot)Wine Quality - Data Visualization
Dataset
The Wine Quality dataset consists of the chemical properties of both red and white variants of Portuguese ‘Vinho Verde’ wine. It covers features such as alcohol content and acidity levels, alongside quality ratings. This dataset is commonly employed to investigate the relationships between these chemical properties and the perceived quality of the wines. For more details or to access the dataset, you can visit the UCI Machine Learning Repository at Wine Quality Dataset.
Libraries
Exploring Data
white_wine <- read.csv('winequality_white.csv', sep = ';')
red_wine <- read.csv('winequality_red.csv', sep = ';')The White Wine dataset has 4898 entries, while the Red Wine dataset has 1599 entries. To streamline analysis and leverage shared features, these datasets have been merged into a unified dataset.
red_wine$type <- "Red"
white_wine$type <- "White"
wines <- rbind(red_wine, white_wine)str(wines)'data.frame': 6497 obs. of 13 variables:
$ fixed.acidity : num 7.4 7.8 7.8 11.2 7.4 7.4 7.9 7.3 7.8 7.5 ...
$ volatile.acidity : num 0.7 0.88 0.76 0.28 0.7 0.66 0.6 0.65 0.58 0.5 ...
$ citric.acid : num 0 0 0.04 0.56 0 0 0.06 0 0.02 0.36 ...
$ residual.sugar : num 1.9 2.6 2.3 1.9 1.9 1.8 1.6 1.2 2 6.1 ...
$ chlorides : num 0.076 0.098 0.092 0.075 0.076 0.075 0.069 0.065 0.073 0.071 ...
$ free.sulfur.dioxide : num 11 25 15 17 11 13 15 15 9 17 ...
$ total.sulfur.dioxide: num 34 67 54 60 34 40 59 21 18 102 ...
$ density : num 0.998 0.997 0.997 0.998 0.998 ...
$ pH : num 3.51 3.2 3.26 3.16 3.51 3.51 3.3 3.39 3.36 3.35 ...
$ sulphates : num 0.56 0.68 0.65 0.58 0.56 0.56 0.46 0.47 0.57 0.8 ...
$ alcohol : num 9.4 9.8 9.8 9.8 9.4 9.4 9.4 10 9.5 10.5 ...
$ quality : int 5 5 5 6 5 5 5 7 7 5 ...
$ type : chr "Red" "Red" "Red" "Red" ...
Variables Distribution
Type
The Type feature is a categorical variable indicating the type of wine, with possible values ‘Red’ and ‘White’.
df_percentage <- (wines |>
group_by(type) |>
summarise(percentage = n() / nrow(wines) * 100))
ggplot(df_percentage, aes(x = type, y=percentage, fill = type)) +
geom_bar(stat = "identity") +
geom_text(aes(label = sprintf("%.1f%%", percentage)),
position = position_stack(vjust = 0.5)) +
labs(title = "Proportion of Red and White Wine",
x = NULL,
y = "Percentage")+
scale_y_continuous(labels = scales::percent_format(scale = 1))+
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))+
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) Quality
The Quality feature in the Wine dataset represents a discrete score between 0 and 10 assigned to each wine sample based on evaluations by wine specialists.
total_quality_distribution <- wines |>
group_by(quality)|>
summarise(total_count = n()) |>
mutate(percentage = round(total_count / sum(total_count) * 100, 1),
type = "Total")
wines |>
group_by(type, quality) |>
summarise(count = n()) |>
mutate(percentage = round(count / sum(count) * 100, 1)) |>
ggplot(aes(x = as.factor(quality), y = percentage, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
geom_line(data = total_quality_distribution, aes(x = as.factor(quality), y = percentage, group = type, color = type), size = 1.5) +
labs(title = "Quality Distribution",
x = NULL,
y = "Percentage") +
scale_y_continuous(labels = scales::percent_format(scale = 1)) +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
scale_color_manual(values = c("Total" = "black")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Fixed Acidity
Fixed acidity refers to acids that do not evaporate readily and are generally stable in the wine. It contributes to the overall acidity of the wine and affects its taste, balance, and stability.
fixed_acidity_frequency_per_type <- wines |>
group_by(type, `fixed.acidity`) |>
count()
ggplot(fixed_acidity_frequency_per_type, aes(x = `fixed.acidity`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Fixed Acidity Distribution by Type",
x = expression("Fixed Acidity (g/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) wines$quality <- as.factor(wines$quality)
ggplot(wines, aes(x = quality, y = `fixed.acidity`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Fixed Acidity Distribution by Quality",
x = NULL,
y = expression("Fixed Acidity (g/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Volatile Acidity
Volatile acidity measures the amount of acetic acid in the wine. Too high levels of volatile acidity can result in an unpleasant, vinegar-like taste.
volatile_acidity_frequency_per_type <- wines |>
group_by(type, `volatile.acidity`) |>
count()
ggplot(volatile_acidity_frequency_per_type, aes(x = `volatile.acidity`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Volatile Acidity Distribution by Type",
x = expression("Volatile Acidity (g/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) ggplot(wines, aes(x = quality, y = `volatile.acidity`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Volatile Acidity Distribution by Quality",
x = NULL,
y = expression("Volatile Acidity (g/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Citric Acid
Citric acid is found in small quantities in wine. It can add freshness and enhance the flavor of wines.
citric_acid_frequency_per_type <- wines |>
group_by(type, `citric.acid`) |>
count()
ggplot(citric_acid_frequency_per_type, aes(x = `citric.acid`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Citric Acid Distribution by Type",
x = expression("Citric Acid (g/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) ggplot(wines, aes(x = quality, y = `citric.acid`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Citric Acid Distribution by Quality",
x = NULL,
y = expression("Citric Acid (g/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Residual Sugar
Residual Sugar indicates the amount of sugar remaining after fermentation stops. Wines with less than 1 gram/liter are rare, while wines with greater than 45 grams/liter are considered sweet.
residual_sugar_frequency_per_type <- wines |>
group_by(type, `residual.sugar`) |>
count()
ggplot(residual_sugar_frequency_per_type, aes(x = `residual.sugar`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Residual Sugar Distribution by Type",
x = expression("Residual Sugar (g/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `residual.sugar`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Residual Sugar Distribution by Quality",
x = NULL,
y = expression("Residual Sugar (g/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Chlorides
Chlorides quantifies the amount of salt in the wine.
chlorides_frequency_per_type <- wines |>
group_by(type, `chlorides`) |>
count()
ggplot(chlorides_frequency_per_type, aes(x = `chlorides`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Chlorides Distribution by Type",
x = expression("Chlorides (g/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `chlorides`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Chlorides Distribution by Quality",
x = NULL,
y = expression("Chlorides (g/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Free Sulfur Dioxide
The free form of SO2 exists in equilibrium between molecular SO2 (as a dissolved gas) and bisulfite ion. It prevents microbial growth and the oxidation of wine.
free_so2_frequency_per_type <- wines |>
group_by(type, `free.sulfur.dioxide`) |>
count()
ggplot(free_so2_frequency_per_type, aes(x = `free.sulfur.dioxide`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Free SO2 by Type",
x = expression("Free SO2 (mg/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `free.sulfur.dioxide`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Free SO2 Distribution by Quality",
x = NULL,
y = expression("Free SO2 (mg/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Total Sulfur Dioxide
This includes both free and bound forms of SO2. In low concentrations, SO2 is generally undetectable, but when free SO2 concentrations exceed 50 ppm, its presence becomes noticeable in both the aroma and taste of the wine.
total_so2_frequency_per_type <- wines |>
group_by(type, `total.sulfur.dioxide`) |>
count()
ggplot(total_so2_frequency_per_type, aes(x = `total.sulfur.dioxide`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Total SO2 by Type",
x = expression("Total SO2 (mg/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `total.sulfur.dioxide`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Total SO2 Distribution by Quality",
x = NULL,
y = expression("Total SO2 (mg/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Density
This feature indicates the mass per unit volume of the wine, measured in grams per cubic centimeter (g/cm³). It reflects the concentration of components like alcohol and sugar, offering insights into the wine’s overall composition.
density_frequency_per_type <- wines |>
group_by(type, `density`) |>
count()
ggplot(density_frequency_per_type, aes(x = `density`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Density by Type",
x = expression("Density (g/cm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `density`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Density Distribution by Quality",
x = NULL,
y = expression("Density (g/cm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) pH
pH describes how acidic or basic a wine is on a scale from 0 (very acidic) to 14 (very basic). Most wines are between 3-4 on the pH scale.
ph_frequency_per_type <- wines |>
group_by(type, `pH`) |>
count()
ggplot(ph_frequency_per_type, aes(x = `pH`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "pH by Type",
x = "pH",
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `pH`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "pH Distribution by Quality",
x = NULL,
y ="pH") +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Sulphates
A wine additive which can contribute to sulfur dioxide gas (S02) levels, wich acts as an antimicrobial and antioxidant.
sulphates_frequency_per_type <- wines |>
group_by(type, `sulphates`) |>
count()
ggplot(sulphates_frequency_per_type, aes(x = `sulphates`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Sulphates by Type",
x = expression("Sulphates (g/dm"^3*")"),
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `sulphates`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Sulphates Distribution by Quality",
x = NULL,
y = expression("Sulphates (g/dm"^3*")")) +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Alcohol
The percent alcohol content of the wine.
alcohol_frequency_per_type <- wines |>
group_by(type, `alcohol`) |>
count()
ggplot(alcohol_frequency_per_type, aes(x = `alcohol`, y = n, fill = type)) +
geom_bar(stat = "identity", position = "dodge", alpha = 0.7) +
labs(title = "Alcohol by Type",
x = "Alcohol (% by volume)",
y = "Frequency") +
scale_fill_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5))ggplot(wines, aes(x = quality, y = `alcohol`, color = type)) +
geom_jitter(alpha = 0.7, width = 0.3) +
labs(title = "Alcohol Distribution by Quality",
x = NULL,
y ="Alcohol (% by volume)") +
scale_color_manual(values = c("Red" = "#b11226", "White" = "#EFEF26")) +
theme(axis.title.y = element_text(margin = margin(r = 10)),
axis.text.x = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5)) Exploring correlations
wines$quality <- as.numeric(as.character(wines$quality))
column_order <- c("fixed.acidity", "volatile.acidity", "citric.acid",
"residual.sugar", "chlorides", "free.sulfur.dioxide",
"total.sulfur.dioxide", "density", "pH", "sulphates",
"alcohol", "quality")
numeric_wines <- wines[, column_order]
cor_matrix <- cor(numeric_wines)
corrplot(cor_matrix, method = "color", type = "lower", tl.col = "black", tl.srt = 45)TODO
- Handling Outliers
- BOX Plots
- Validaiting the relationship between related variables
- Validating the impact of each variable on Quality