# Load necessary packages
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
#install.packages("viridis")
library(viridis)
## Warning: package 'viridis' was built under R version 4.3.2
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.3.2
# Read CSV files
data1 <- read.csv("updated_section_3_with_ID.csv")
data2 <- read.csv("updated_section_5_with_ID.csv")

# Combine the datasets
merged_data <- merge(data1, data2, by = "ID")

# Load necessary packages
# Graph 1: Interactive Scatter Plot - Comparison of q1 and q6
scatter_plot <- ggplot(merged_data, aes(x = factor(q1), y = factor(q6), color = factor(q6))) +
  geom_point() +
  labs(title = "Interactive Scatter Plot: Impact of AI on IT Service Delivery vs AI Integration",
       x = "AI Integration (q1)",
       y = "Impact on IT Service Delivery (q6)",
       color = "Impact on IT Service Delivery (q6)") +
  theme_minimal() +
  scale_color_viridis(discrete = TRUE)

# Convert to plotly object for interactivity
#interactive_scatter_plot <- ggplotly(scatter_plot)

#interactive_scatter_plot

# Insight: The scatter plot shows a positive correlation between AI integration (q1) and its impact on IT service delivery (q6). Organizations with higher AI integration tend to experience a more positive impact on service delivery.


# Graph 2: Bar Plot - Comparison of q2 and q7
ggplot(merged_data, aes(x = factor(q2), fill = factor(q7))) +
  geom_bar(position = "dodge") +
  labs(title = "Bar Plot: User Satisfaction Changes vs Identified AI Applications",
       x = "Identified AI Applications (q2)",
       y = "Count",
       fill = "User Satisfaction Changes (q7)") +
  theme_minimal()

# Insight: The bar plot suggests a potential relationship between user satisfaction changes (q7) and the identification of specific AI applications (q2). Different levels of identified AI applications may lead to varying user satisfaction changes.

# Graph 3: Box Plot - Comparison of q3 and q8
ggplot(merged_data, aes(x = factor(q3), y = q8)) +
  geom_boxplot(color = "skyblue", fill = "lightblue", outlier.colour = "red", outlier.shape = 16, size = 0.7) +
  stat_summary(fun = "mean", geom = "point", shape = 17, size = 3, color = "red", position = position_dodge(width = 0.75)) +
  labs(
    title = "Box Plot: AI Impact on Scalability vs Role in Predictive Maintenance",
    x = "AI Impact on Scalability (q3)",
    y = "AI Role in Predictive Maintenance (q8)"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels
    panel.grid.major = element_line(color = "lightgray", linetype = "dashed"),  # Add dashed grid lines
    plot.title = element_text(hjust = 0.5),  # Center the title
    plot.margin = margin(1, 1, 1, 1, "cm")  # Adjust plot margins
  )

# Insight: This box plot indicates a possible link between the positive impact of AI on scalability (q3) and its significant role in predictive maintenance (q8). Different levels of AI impact on scalability may result in varying roles in predictive maintenance.

# Graph 4: Scatter Plot - Comparison of q4 and q9
library(ggplot2)

ggplot(merged_data, aes(x = q4, y = q9)) +
  geom_point(color = "blue", size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE, color = "red", linetype = "dashed", size = 1) +  # Adding a linear regression line
  labs(
    title = "Scatter Plot: AI Optimization of Resource Allocation vs Success Measurement",
    x = "AI Optimization of Resource Allocation (q4)",
    y = "Success Measurement of AI Implementations (q9)"
  ) +
  theme_minimal() +
  theme(
    axis.text = element_text(size = 10),  # Adjusting axis text size
    axis.title = element_text(size = 12),  # Adjusting axis title size
    plot.title = element_text(hjust = 0.5, size = 14),  # Center the title and adjust the size
    plot.margin = margin(1, 1, 1, 1, "cm")  # Adjust plot margins
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

# Insight: The scatter plot reveals a potential connection between AI optimization of resource allocation (q4) and the success measurement of AI implementations (q9). Instances of AI optimizing resource allocation may contribute to the success of AI implementations.

# Graph 5: Bar Plot - Comparison of q5 and q10
library(ggplot2)

ggplot(merged_data, aes(x = factor(q5), fill = factor(q10))) +
  geom_bar(position = "dodge", color = "black", show.legend = TRUE) +
  geom_text(
    aes(label = stat(count)),
    position = position_dodge(width = 0.9),
    vjust = -0.5, 
    size = 3,
    color = "black",
    stat = "count"
  ) +
  labs(
    title = "Bar Plot: Measures for Data Security vs AI-related Challenges",
    x = "Measures for Data Security (q5)",
    y = "Count",
    fill = "AI-related Challenges (q10)"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels
    legend.position = "top",  # Move legend to the top
    legend.title = element_blank(),  # Remove legend title
    axis.title = element_text(size = 12),  # Adjust axis title size
    plot.title = element_text(hjust = 0.5, size = 14),  # Center the title and adjust the size
    plot.margin = margin(1, 1, 1, 1, "cm")  # Adjust plot margins
  )
## Warning: `stat(count)` was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Graph 8: Pie Chart - Comparison of q3 and q8
ggplot(merged_data, aes(x = "", fill = factor(q7))) +
  geom_bar(width = 1, color = "white") +
  coord_polar(theta = "y") +
  labs(title = "Pie Chart: Distribution of AI Impact on Scalability",
       fill = "AI Impact on Scalability (q3)") +
  theme_minimal()

# Insight: The pie chart illustrates the distribution of AI impact on scalability (q3) in a circular format, offering a concise summary of the data.

# Graph 9: Density Plot - Comparison of q4 and q9
# Install and load necessary libraries
#install.packages("tidyverse")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'tibble' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Assuming 'merged_data' is your data frame

# Enhance the density plot
ggplot(merged_data, aes(x = q4, fill = factor(q9))) +
  geom_density(alpha = 0.5) +
  labs(
    title = "Density Plot: AI Optimization of Resource Allocation vs Success Measurement",
    x = "AI Optimization of Resource Allocation (q4)",
    y = "Density",
    fill = "Success Measurement of AI Implementations (q9)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5),  # Center the title
    legend.position = "top",  # Move legend to the top
    legend.title = element_text(face = "bold"),  # Bold legend title
    legend.text = element_text(size = 10),  # Adjust legend text size
    axis.text = element_text(size = 10),  # Adjust axis text size
    axis.title = element_text(face = "bold")  # Bold axis title
  )

# Insight: The density plot provides a smooth representation of the distribution of data points, highlighting potential patterns between AI optimization of resource allocation (q4) and the success measurement of AI implementations (q9).

# Graph 12: Violin Plot - Comparison of q2 and q12
# Install and load necessary libraries
#install.packages("tidyverse")
library(tidyverse)

# Assuming 'merged_data' is your data frame

# Enhance the violin plot
ggplot(merged_data, aes(x = factor(q2), y = q10, fill = factor(q2))) +
  geom_violin(trim = FALSE, scale = "width", width = 0.8, alpha = 0.7) +
  labs(
    title = "Violin Plot: Distribution of AI Applications and IT Service Quality",
    x = "Identified AI Applications (q2)",
    y = "IT Service Quality (q12)",
    fill = "Identified AI Applications (q2)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5),  # Center the title
    legend.position = "top",  # Move legend to the top
    legend.title = element_text(face = "bold"),  # Bold legend title
    legend.text = element_text(size = 10),  # Adjust legend text size
    axis.text = element_text(size = 10),  # Adjust axis text size
    axis.title = element_text(face = "bold")  # Bold axis title
  )

# Graph 14: Stacked Bar Chart - Comparison of q4 and q14
# Install and load necessary libraries
# install.packages("tidyverse")
library(tidyverse)

# Assuming 'merged_data' is your data frame

# Enhance the stacked bar chart
ggplot(merged_data, aes(x = factor(q4), fill = factor(q9))) +
  geom_bar(position = "stack", stat = "count", color = "white") +
  labs(
    title = "Stacked Bar Chart: AI Optimization of Resource Allocation vs Team Collaboration",
    x = "AI Optimization of Resource Allocation (q4)",
    fill = "Team Collaboration (q14)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5),  # Center the title
    legend.position = "top",  # Move legend to the top
    legend.title = element_text(face = "bold"),  # Bold legend title
    legend.text = element_text(size = 10),  # Adjust legend text size
    axis.text = element_text(size = 10),  # Adjust axis text size
    axis.title = element_text(face = "bold")  # Bold axis title
  )

# Insight: The area chart visualizes the relationship between measures for data security (q5) and AI-related challenges (q10), providing a sense of the cumulative impact.

# Final Conclusion Comment:
# Overall, the diverse set of graphs provides a comprehensive view of the impact of AI in the IT industry. The visualizations highlight relationships between different aspects of AI integration and its consequences on IT processes, user satisfaction, and data security, among other factors.

# the analysis of these graphs suggests a strong correlation between AI integration in IT infrastructure and its positive impact on various aspects of IT services. This alignment underscores the potential benefits of AI adoption in enhancing IT processes, scalability, user satisfaction, and predictive maintenance in organizations.
# The interactive visualizations reveal valuable insights into the impact of AI in the IT industry. 
# Graphs 1-5 showcase the distribution and relationships between key AI-related variables, 
# providing a comprehensive overview of AI integration, application identification, scalability, 
# resource optimization, and data security measures. Interactive features enhance exploration, 
# enabling users to delve into specific data points. Overall, the analysis suggests a positive 
# influence of AI on various aspects of IT, with user satisfaction, issue resolution, and team 
# collaboration being notable areas of impact.