Part 1

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)

Part 2

  1. How does the distribution of diamond prices differ among various diamond cuts? # Create a bar plot showing price distribution for each cut
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")

  1. Is there a correlation between carat size and the quality of the cut? # Create a scatter plot to explore the relationship between carat and cut quality
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")

  1. How do the dimensions (x, y, and z) of diamonds vary across different cut qualities? # Create violin plots to show the distribution of dimensions by 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")

  1. Is there a relationship between the clarity of a diamond and its price? # Create a scatter plot to explore the relationship between clarity and price
ggplot(diamonds, aes(x = clarity, y = price)) +
  geom_boxplot(fill = "green", color = "black") +
  labs(title = "Price Distribution by Clarity", x = "Clarity", y = "Price")

Part 3

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)

Create a bar plot to visualize the average price by color and clarity

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()

Part 4

  • A visual summary of at least 5 columns of your data
  • This should include distributions at least
  • In addition, you should consider trends, correlations, and interactions between variables
  • Use different channels to show how categorical variables interact with continuous variables

Visual Summary of Columns

1. Distribution of Carat

ggplot(diamonds, aes(x = carat)) +
  geom_histogram(binwidth = 0.1, fill = "blue", color = "black") +
  labs(title = "Distribution of Carat", x = "Carat", y = "Frequency")

2. Price vs. Carat

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point(alpha = 0.5, color = "blue") +
  labs(title = "Price vs. Carat", x = "Carat", y = "Price")

3. Boxplot of Price by Cut

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")

4. Color vs. Price

ggplot(diamonds, aes(x = color, y = price, fill = color)) +
  geom_boxplot() +
  labs(title = "Color vs. Price", x = "Color", y = "Price") +
  theme(legend.position = "none")

5. Scatterplot Matrix for Carat, Price, and Clarity

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?