library(ggplot2)
data("diamonds")
# Load the diamonds dataset
data(diamonds)
# Subset the data to include at least 9 columns (adjust as needed)
selected_columns <- diamonds[, c("carat", "depth", "table", "price", "x", "y", "z", "cut", "color", "clarity")]
# Define a function to create summary plots for numeric columns
create_numeric_summary_plot <- function(column) {
ggplot(selected_columns, aes_string(x = column)) +
geom_histogram(binwidth = 0.1, fill = "blue", color = "black") +
labs(title = paste("Distribution of", column), x = column, y = "Frequency") +
facet_wrap(~1, ncol = 3)
}
# Define a function to create summary plots for categorical columns
create_categorical_summary_plot <- function(column) {
ggplot(selected_columns, aes_string(x = column, fill = factor(selected_columns[[column]]))) +
geom_bar() +
labs(title = paste("Distribution of", column), x = column, y = "Count") +
theme(legend.position = "top") +
facet_wrap(~1, ncol = 3)
}
# Create summary plots for numeric columns
numeric_summary_plots <- lapply(names(selected_columns[, 1:7]), create_numeric_summary_plot)
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Create summary plots for categorical columns
categorical_summary_plots <- lapply(names(selected_columns[, 8:10]), create_categorical_summary_plot)
# Combine all summary plots
all_summary_plots <- c(numeric_summary_plots, categorical_summary_plots)
# Arrange and display the summary plots
library(gridExtra)
grid.arrange(grobs = all_summary_plots, ncol = 3)
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
geom_boxplot() +
labs(title = "Price Distribution by Cut", x = "Cut", y = "Price") +
theme(legend.position = "none")
ggplot(diamonds, aes(x = carat, y = as.numeric(cut))) +
geom_point() +
labs(title = "Scatter Plot of Carat vs. Cut Quality", x = "Carat", y = "Cut Quality")
ggplot(diamonds, aes(x = cut, y = x, fill = cut)) +
geom_violin() +
labs(title = "Distribution of x by Cut Quality", x = "Cut", y = "x") +
theme(legend.position = "none")
ggplot(diamonds, aes(x = cut, y = y, fill = cut)) +
geom_violin() +
labs(title = "Distribution of y by Cut Quality", x = "Cut", y = "y") +
theme(legend.position = "none")
ggplot(diamonds, aes(x = cut, y = z, fill = cut)) +
geom_violin() +
labs(title = "Distribution of z by Cut Quality", x = "Cut", y = "z") +
theme(legend.position = "none")
ggplot(diamonds, aes(x = clarity, y = price)) +
geom_boxplot(fill = "green", color = "black") +
labs(title = "Price Distribution by Clarity", x = "Clarity", y = "Price")
Use of aggregation functions # Calculate the average price of diamonds for each combination of color and clarity
price_summary <- aggregate(price ~ color + clarity, data = diamonds, FUN = mean)
ggplot(price_summary, aes(x = color, y = price, fill = clarity)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Average Price of Diamonds by Color and Clarity",
x = "Color",
y = "Average Price") +
scale_fill_brewer(palette = "Set3") +
theme_minimal()
ggplot(diamonds, aes(x = carat)) +
geom_histogram(binwidth = 0.1, fill = "blue", color = "black") +
labs(title = "Distribution of Carat", x = "Carat", y = "Frequency")
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(alpha = 0.5, color = "blue") +
labs(title = "Price vs. Carat", x = "Carat", y = "Price")
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
geom_boxplot() +
labs(title = "Boxplot of Price by Cut", x = "Cut", y = "Price") +
theme(legend.position = "none")
ggplot(diamonds, aes(x = color, y = price, fill = color)) +
geom_boxplot() +
labs(title = "Color vs. Price", x = "Color", y = "Price") +
theme(legend.position = "none")
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
ggpairs(diamonds[, c("carat", "price", "clarity")], title = "Scatterplot Matrix")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Numeric Summary of Data:
Insight: We obtained key statistics for numeric columns, including min, max, mean, and quantiles. Significance: This helps us understand the central tendency and distribution of numeric data. Further Questions: Are there outliers in numeric columns? How do numeric attributes relate to the project’s goals? Set of Novel Questions:
Insight: We formulated novel questions based on column summaries, data documentation, and project goals. Significance: These questions guide our exploration and analysis, helping us uncover insights relevant to the project. Further Questions: How can we use these questions to drive actionable insights and decisions? Use of Aggregation Functions:
Insight: We used aggregation functions to analyze relationships within the data, such as average prices by color and clarity. Significance: This helps us identify patterns or trends that may not be immediately apparent in raw data. Further Questions: How do these aggregations vary over time, if applicable? Can we create more sophisticated aggregations for deeper insights? Visual Summary of Data:
Insight: We created visualizations to explore distributions, trends, and correlations in the data. Significance: Visualizations provide a more intuitive understanding of data patterns. Further Questions: Are there hidden patterns or outliers in the data? How can we leverage these visualizations to inform decision-making?