# Set seed for reproducibility
set.seed(123)

# Generate hypothetical data for Textile Craft Manufacturing Co.
textile_data <- data.frame(
  Product_Category = rep(c("Fabrics", "Apparel", "Accessories"), each = 3),
  Subcategory = rep(c("Cotton", "Silk", "Wool", "Casual Wear", "Formal Wear", "Sportswear", "Scarves", "Hats", "Bags"), each = 1),
  Batch_ID = rep(1:3, each = 3),
  Profitability = round(runif(9, min = 55, max = 80), 2),
  Material_Usage_Percentage = round(runif(9, min = 60, max = 90), 2),
  Material_Usage_Efficiency = round(runif(9, min = 0.6, max = 0.9), 2),
  Production_Time = round(runif(9, min = 15, max = 50)),
  Energy_Consumption = round(runif(9, min = 50, max = 200), 2),
  Compliance_Score = round(runif(9, min = 70, max = 100)),
  Communication = round(runif(9, min = 20, max = 80), 2),
  Dimensional_Accuracy = round(runif(9, min = 90, max = 98), 2),
  Surface_Finish = round(runif(9, min = 13, max = 15), 2),
  Material_Strength = round(runif(9, min = 80, max = 95)),
  Production_Speed = round(runif(9, min = 50, max = 120)),
  Defect_Rates = round(runif(9, min = 10, max = 15), 2),
  Production_Efficiency = round(runif(9, min = 0.7, max = 0.9), 2),
  Energy_Consumption_Unit = round(runif(9, min = 24, max = 80), 2),
  Customer_Satisfaction = round(runif(9, min = 25, max = 75)),
  Delivery_Time = round(runif(9, min = 45, max = 95), 2),
  Quality_Rating = round(runif(9, min = 20, max = 100)),
  Cost_Effectiveness = round(runif(9, min = 15, max = 75), 2),
  Reliability = round(runif(9, min = 80, max = 95)),
  Flexibility = round(runif(9, min = 25, max = 50), 2),
  Production_Volume_Process = round(runif(9, min = 1000, max = 5000)),
  Energy_Consumption_Process = round(runif(9, min = 500, max = 2000)),
  Production_Time_Process = round(runif(9, min = 20, max = 80))
)

# Select numeric columns for standardization
numeric_cols <- sapply(textile_data, is.numeric)
numeric_data <- textile_data[, numeric_cols]

# Standardize numeric columns using z-score
standardized_data <- scale(numeric_data)

# Display the generated data
print(textile_data)
##   Product_Category Subcategory Batch_ID Profitability Material_Usage_Percentage
## 1          Fabrics      Cotton        1         62.19                     73.70
## 2          Fabrics        Silk        1         74.71                     88.71
## 3          Fabrics        Wool        1         65.22                     73.60
## 4          Apparel Casual Wear        2         77.08                     80.33
## 5          Apparel Formal Wear        2         78.51                     77.18
## 6          Apparel  Sportswear        2         56.14                     63.09
## 7      Accessories     Scarves        3         68.20                     86.99
## 8      Accessories        Hats        3         77.31                     67.38
## 9      Accessories        Bags        3         68.79                     61.26
##   Material_Usage_Efficiency Production_Time Energy_Consumption Compliance_Score
## 1                      0.70              36             163.77               74
## 2                      0.89              25              82.46               77
## 3                      0.87              20              97.73               84
## 4                      0.81              49              84.74               78
## 5                      0.79              47              71.42               96
## 6                      0.90              39             112.18               71
## 7                      0.80              43             112.06               83
## 8                      0.81              16             105.33               94
## 9                      0.76              32              72.87               74
##   Communication Dimensional_Accuracy Surface_Finish Material_Strength
## 1         53.66                92.20          14.42                90
## 2         32.39                96.52          13.00                86
## 3         27.65                93.59          13.95                92
## 4         65.20                96.48          13.44                82
## 5         73.70                96.50          13.76                87
## 6         42.47                96.35          14.23                95
## 7         59.91                93.52          13.70                93
## 8         25.69                96.04          13.22                93
## 9         43.04                95.03          13.49                83
##   Production_Speed Defect_Rates Production_Efficiency Energy_Consumption_Unit
## 1               59        12.56                  0.78                   77.43
## 2               96        13.00                  0.73                   56.79
## 3               74        11.66                  0.89                   46.65
## 4               96        12.44                  0.76                   60.28
## 5               72        14.77                  0.71                   41.91
## 6               63        12.41                  0.89                   41.23
## 7              105        14.45                  0.84                   36.31
## 8               57        14.57                  0.73                   44.69
## 9               83        13.04                  0.81                   79.12
##   Customer_Satisfaction Delivery_Time Quality_Rating Cost_Effectiveness
## 1                    33         77.99             87              29.79
## 2                    30         86.09             38              21.67
## 3                    32         84.31             39              38.40
## 4                    60         93.99             26              49.32
## 5                    56         66.97             40              28.01
## 6                    70         60.59             79              41.69
## 7                    59         65.47             88              28.08
## 8                    62         45.52             60              45.14
## 9                    51         54.19             51              36.23
##   Reliability Flexibility Production_Volume_Process Energy_Consumption_Process
## 1          90       29.60                      4359                       1853
## 2          86       46.59                      2250                        911
## 3          85       43.66                      3833                        982
## 4          88       41.71                      2060                       1978
## 5          91       40.45                      3377                       1430
## 6          83       34.31                      2925                       1906
## 7          86       38.25                      2060                       1200
## 8          84       46.87                      3258                       1110
## 9          89       39.54                      4653                       1489
##   Production_Time_Process
## 1                      29
## 2                      54
## 3                      34
## 4                      78
## 5                      56
## 6                      51
## 7                      44
## 8                      73
## 9                      42
# 1- Multivariate Scatter Plot Matrix (SPLOM)
# Library
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
# Selecting Variables for SPLOM
splo_variables <- c("Dimensional_Accuracy", "Surface_Finish", "Material_Strength", "Production_Speed", "Defect_Rates")

#Spol Plot
splo_plot <- ggpairs(
  textile_data,
  columns = splo_variables,
  title = "Scatter Plot Matrix (SPLOM)",
  upper = list(continuous = "points", combo = "box"),
  lower = list(continuous = "points", combo = "dot"),
  diag = list(continuous = "barDiag", combo = "barDiag")
)
# Display the SPLOM
splo_plot
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# 2- Parallel Coordinate plot
# Library
library(MASS)

# Select relevant columns for the parallel coordinate plot
parallel_data <- textile_data[, c("Subcategory", "Production_Time", "Defect_Rates", "Material_Usage_Efficiency", 
                                  "Energy_Consumption", "Customer_Satisfaction", "Compliance_Score")]

# Define colors for each category
category_colors <- c("red", "green", "blue","black","yellow","brown","violet","pink","orange")

# Create the parallel coordinate plot
parcoord(log(parallel_data[, -1]), col = category_colors[as.numeric(factor(parallel_data$Subcategory))], lty = 1, lwd = 2)

# Adding a legend
legend("topright", legend = levels(factor(parallel_data$Subcategory)), fill = category_colors, cex = 0.8)

# 3- Heatmap
# Library
library("heatmaply")
## Loading required package: plotly
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:MASS':
## 
##     select
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
## 
## Loading required package: viridis
## Loading required package: viridisLite
## 
## ======================
## Welcome to heatmaply version 1.5.0
## 
## Type citation('heatmaply') for how to cite the package.
## Type ?heatmaply for the main documentation.
## 
## The github page is: https://github.com/talgalili/heatmaply/
## Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues
## You may ask questions at stackoverflow, use the r and heatmaply tags: 
##   https://stackoverflow.com/questions/tagged/heatmaply
## ======================
# Select relevant columns for the heatmap
heatmap_data <- textile_data[, c("Dimensional_Accuracy", "Surface_Finish", "Material_Strength", "Production_Speed", "Defect_Rates")]

# Normalize the data
normalized_data <- as.data.frame(scale(heatmap_data))

# Create heatmap using heatmaply
heatmaply(normalized_data, main = "Heatmap")
# 4- Radial Chart or Star plot 
# Library
library(fmsb)

radar_data <- textile_data[, c("Delivery_Time", "Quality_Rating", "Cost_Effectiveness", "Reliability", "Flexibility", "Communication")]

# Convert the radar_data to a format suitable for radar Chart
radar_data <- rbind(rep(100, ncol(radar_data)), rep(0, ncol(radar_data)), radar_data)

# Create the radar chart
radarchart(radar_data, axistype = 1, title = "Radar Chart")

# 5- Bubble Chart
# Library
library(ggplot2)

# Selecting relevant columns for the bubble chart
bubble_data <- textile_data[, c("Production_Time_Process", "Energy_Consumption_Process", "Production_Volume_Process", "Material_Usage_Efficiency")]

# Define the plot using ggplot2
bubble_plot <- ggplot(bubble_data, aes(x = Production_Volume_Process, y = Production_Time_Process, size = Material_Usage_Efficiency, color = Energy_Consumption_Process) )+
  geom_point(alpha = 0.7) +  # Use alpha to add transparency
  scale_size_continuous(range = c(3, 10)) +  # Adjust size range
  scale_color_gradient(low = "blue", high = "red") +  # Adjust color gradient
  labs(title = "Bubble Chart",
       x = "Production Volume",
       y = "Production Time",
       size = "Material Usage Efficiency",
       color = "Energy Consumption")

# Show the plot
print(bubble_plot)

# 6-3D Scatter Plot
# Selecting relevant columns for the new 3D scatter plot
scatter_data_process <- textile_data[, c("Production_Volume_Process", "Production_Time_Process", "Energy_Consumption_Process")]

# Define the new 3D scatter plot using plot_ly
scatter_plot_process <- plot_ly(
  data = scatter_data_process,
  x = ~Energy_Consumption_Process,
  y = ~Production_Volume_Process,
  z = ~Production_Time_Process,
  type = "scatter3d",
  mode = "markers",
  marker = list(size = 10, opacity = 0.7, color = ~Energy_Consumption_Process, colorscale = "Viridis")
) %>% 
  layout(scene = list(
    xaxis = list(title = "Energy Consumption Process"),
    yaxis = list(title = "Production Volume Process"),
    zaxis = list(title = "Production Time Process")
  )) %>% 
  colorbar(title = "3D Scatter plot")
## Warning: Didn't find a colorbar to modify.
# Show the new 3D scatter plot
scatter_plot_process
# 7- Tree Map
# Library
library(plotly)

# Modify the data based on your actual columns
treemap_data <- textile_data[, c("Subcategory", "Production_Volume_Process", "Product_Category")]

# Create the treemap
fig <- plot_ly(
  type = "treemap",
  labels = treemap_data$Subcategory,
  parents = "",
  values = treemap_data$Production_Volume_Process,
  branchvalues = "total",  # Change to 'remainder' for a squarified treemap
  hoverinfo = "label+value",
  colors = treemap_data$Product_Category
)

# Show the treemap
fig
# 8- Violin Plot
# Library
library(ggplot2)

# Create the violin plot
ggplot(textile_data, aes(x = Product_Category, y = Defect_Rates, fill = factor(Batch_ID))) +
  geom_violin(scale = "width", trim = FALSE) +
  geom_boxplot(width = 0.2, fill = "white", color = "black") +
  labs(title = "Violin Plot",
       x = "Product Category",
       y = "Defect Rates",
       fill = "Batch ID") +
  theme_minimal()

# 9- Ternary Plot
# Library
library(ggtern)
## Registered S3 methods overwritten by 'ggtern':
##   method           from   
##   grid.draw.ggplot ggplot2
##   plot.ggplot      ggplot2
##   print.ggplot     ggplot2
## --
## Remember to cite, run citation(package = 'ggtern') for further info.
## --
## 
## Attaching package: 'ggtern'
## 
## The following objects are masked from 'package:ggplot2':
## 
##     aes, annotate, ggplot, ggplot_build, ggplot_gtable, ggplotGrob,
##     ggsave, layer_data, theme_bw, theme_classic, theme_dark,
##     theme_gray, theme_light, theme_linedraw, theme_minimal, theme_void
# Create a ternary plot
ternary_plot <- ggtern(textile_data, aes(x = Material_Usage_Percentage, y = Profitability, z = Quality_Rating)) +
  geom_point() +
  theme_rgbw() +  # Optional: Add color theme
  labs(title = "Ternary Plot")

# Show the plot
print(ternary_plot)