library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## 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
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
# Generate sample data with a slight correlation for interest
set.seed(123)
data <- data.frame(
  x = rnorm(100),
  y = rnorm(100) + 0.5 * rnorm(100)  # Add some correlation
)

p <- plot_ly(data, x = ~x, y = ~y, type = "scatter", mode = "markers",
             marker = list(size = 8, opacity = 0.7, color = ~y, colorscale = "Viridis"),
             text = ~paste("X:", round(x, 2), "<br>Y:", round(y, 2)),
             hovertemplate = "<b>X: %{x:.2f}</b><br>Y: %{y:.2f}<extra></extra>") %>%
  layout(
    title = list(text = "Interactive Scatter Plot: Exploring Correlations", font = list(size = 20)),
    xaxis = list(title = "X Variable"),
    yaxis = list(title = "Y Variable"),
    showlegend = FALSE
  )
p
# Generate 3D sample data
set.seed(456)
data_3d <- data.frame(
  x = rnorm(100),
  y = rnorm(100),
  z = rnorm(100) + 0.3 * (rnorm(100) + rnorm(100))  # Simulated interaction
)

p3d <- plot_ly(data_3d, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers",
               marker = list(size = 4, opacity = 0.8, color = ~z, colorscale = "Plasma"),
               text = ~paste("X:", round(x, 2), "<br>Y:", round(y, 2), "<br>Z:", round(z, 2)),
               hovertemplate = "<b>X: %{x:.2f}</b><br>Y: %{y:.2f}<br>Z: %{z:.2f}<extra></extra>") %>%
  layout(
    title = list(text = "3D Scatter Plot: Multi-Dimensional Relationships", font = list(size = 20)),
    scene = list(
      xaxis = list(title = "X-Axis"),
      yaxis = list(title = "Y-Axis"),
      zaxis = list(title = "Z-Axis")
    ),
    showlegend = FALSE
  )
p3d
# Bar Chart: Categorical Comparisons
# Sample bar data with categories
bar_data <- data.frame(
  category = c("Technology", "Healthcare", "Finance", "Retail", "Education"),
  value = c(45, 32, 28, 50, 22),
  color = c("#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FFEAA7")
)

pb <- plot_ly(bar_data, x = ~category, y = ~value, type = "bar",
              marker = list(color = ~color),
              text = ~value,
              hovertemplate = "<b>%{x}</b><br>Value: %{y}<extra></extra>") %>%
  layout(
    title = list(text = "Bar Chart: Sector Performance Overview", font = list(size = 20)),
    xaxis = list(title = "Sectors", tickangle = -45),
    yaxis = list(title = "Performance Score"),
    bargap = 0.2
  )
pb
# Box Plot: Distribution Plot
# Box plot data
set.seed(789)
box_data <- data.frame(
  group = rep(c("Control", "Treatment"), each = 50),
  value = c(rnorm(50, mean = 10, sd = 2), rnorm(50, mean = 12, sd = 3))
)

pbox <- plot_ly(box_data, y = ~value, color = ~group, type = "box",
                colors = c("#74B9FF", "#FD79A8"),
                boxpoints = "all", pointpos = 0,
                jitter = 0.2, pointpos = 0,
                text = ~group,
                hovertemplate = "<b>%{text}</b><br>Value: %{y:.2f}<extra></extra>") %>%
  layout(
    title = list(text = "Box Plot: Group Distributions and Outliers", font = list(size = 20)),
    yaxis = list(title = "Values"),
    showlegend = TRUE,
    legend = list(orientation = "h", x = 0.5, y = 1.1)
  )
pbox
# Heat Map: corelation Analysis
# Generate correlation matrix data
set.seed(202)
vars <- 5
mat <- matrix(rnorm(vars^2), nrow = vars)
mat <- cor(mat)  # Correlation matrix

rownames(mat) <- paste("Var", 1:nrow(mat))
colnames(mat) <- paste("Var", 1:ncol(mat))

pheat <- plot_ly(x = colnames(mat), y = rownames(mat), z = ~mat, type = "heatmap",
                 colorscale = "RdBu",
                 hovertemplate = "<b>%{y} vs %{x}</b><br>Correlation: %{z:.3f}<extra></extra>") %>%
  layout(
    title = list(text = "Heatmap: Variable Correlation Matrix", font = list(size = 20)),
    xaxis = list(title = "Variables"),
    yaxis = list(title = "Variables")
  )
pheat
# Pie data
pie_data <- data.frame(
  labels = c("Apples", "Bananas", "Cherries", "Dates", "Elderberries"),
  values = c(30, 25, 20, 15, 10),
  colors = c("#FF9999", "#66B2FF", "#99FF99", "#FFCC99", "#FF99CC")
)

ppie <- plot_ly(pie_data, labels = ~labels, values = ~values, type = "pie",
                marker = list(colors = ~colors),
                textinfo = "label+percent",
                hovertemplate = "<b>%{label}</b><br>Value: %{value}<br>Percentage: %{percent}<extra></extra>") %>%
  layout(
    title = list(text = "Pie Chart: Market Share Distribution", font = list(size = 20)),
    showlegend = TRUE
  )
ppie