🚗 Cars Analysis

MPG Overview

🌡️ Interactive MPG vs Weight

📊 Car Performance Summary

Engine Power

⚙️ Horsepower Distribution

🔢 Power vs Weight Matrix

Gear Analysis

🛞 Gears vs MPG

📈 Gear Performance Table

🌸 Flowers Analysis

Petal Patterns

🌼 Petal Length Distribution

📏 Iris Measurements

Species Comparison

🌺 Sepal vs Petal Scatter

📊 Ratio Analysis

Size Categories

📐 Petal Size Matrix

🔍 Size Statistics

🌿 Plants Analysis

Treatment Effects

📈 Plant Weight Comparison

📋 Treatment Summary

Weight Distribution

📊 Density by Treatment

🔢 Detailed Statistics

Growth Categories

🎯 Weight Category Analysis

📊 Category Breakdown

---
title: "My Dashboard-01"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
    self_contained: true
    theme: united
---

```{r setup, include=FALSE}
library(flexdashboard)
library(plotly)
library(DT)
library(dplyr)
library(ggplot2)
library(tidyr)

# Data Preparation
data("mtcars")
data("iris")
data("PlantGrowth")

#Add calculated columns for interactivity
mtcars <- mtcars %>%
mutate(
MPG_Category = ifelse(mpg > 20, "High", "Low"),
Weight_Category = ifelse(wt > 4, "Heavy", "Light")
)

iris <- iris %>%
mutate(
Petal_Ratio = Petal.Length / Petal.Width,
Sepal_Ratio = Sepal.Length / Sepal.Width
)

PlantGrowth <- PlantGrowth %>%
mutate(
Weight_Category = ifelse(weight > 5, "High", "Low")
)

```

# 🚗 Cars Analysis {.tabset .tabset-fade}

## MPG Overview

### 🌡️ Interactive MPG vs Weight {data-width=500}

```{r}
p1 <- ggplot(mtcars, aes(x = wt, y = mpg, color = MPG_Category,
text = paste("Cyl:", cyl, "
HP:", hp))) +
geom_point(size = 4, alpha = 0.8) +
geom_smooth(method = "lm", se = TRUE, color = "black", alpha = 0.3) +
labs(title = "Fuel Efficiency vs Car Weight",
x = "Weight (1000 lbs)", y = "Miles per Gallon") +
theme_minimal(base_size = 11) +
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 12),
legend.position = "bottom")

ggplotly(p1, tooltip = "text", height = 350, width = NULL)
```

### 📊 Car Performance Summary {data-width=500}

```{r}
car_summary <- mtcars %>%
group_by(cyl, MPG_Category) %>%
summarise(
Count = n(),
Avg_MPG = round(mean(mpg), 1),
Avg_HP = round(mean(hp), 0),
Avg_WT = round(mean(wt), 2),
.groups = 'drop'
)

DT::datatable(car_summary,
options = list(
pageLength = 6,
scrollY = "200px",
scrollCollapse = TRUE,
dom = 't',
autoWidth = TRUE
),
rownames = FALSE,
height = "100%",
fillContainer = TRUE,
class = 'compact')
```


## Engine Power

### ⚙️ Horsepower Distribution {data-width=500}

```{r}
p2 <- ggplot(mtcars, aes(x = factor(cyl), y = hp, fill = factor(cyl))) +
geom_boxplot(alpha = 0.7) +
geom_jitter(width = 0.2, alpha = 0.6, size = 2) +
labs(title = "Horsepower by Cylinder Count",
x = "Cylinders", y = "Horsepower") +
theme_minimal(base_size = 11) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 12))

ggplotly(p2, height = 350)
```


### 🔢 Power vs Weight Matrix {data-width=500}

```{r}
power_summary <- mtcars %>%
mutate(cyl = factor(cyl)) %>%
group_by(cyl, Weight_Category) %>%
summarise(
Count = n(),
Avg_HP = round(mean(hp), 0),
.groups = 'drop'
) %>%
tidyr::pivot_wider(names_from = Weight_Category,
values_from = c(Count, Avg_HP),
values_fill = 0)

DT::datatable(power_summary,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```


## Gear Analysis

### 🛞 Gears vs MPG {data-width=500}

```{r}
p3 <- ggplot(mtcars, aes(x = factor(gear), y = mpg, fill = factor(gear))) +
geom_violin(alpha = 0.6) +
geom_boxplot(width = 0.2, fill = "white") +
labs(title = "MPG by Number of Gears",
x = "Forward Gears", y = "Miles per Gallon") +
theme_minimal(base_size = 11) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 12))

ggplotly(p3, height = 350)
```


### 📈 Gear Performance Table {data-width=500}

```{r}
gear_summary <- mtcars %>%
group_by(gear) %>%
summarise(
Count = n(),
Avg_MPG = round(mean(mpg), 1),
Top_MPG = round(max(mpg), 1),
Avg_HP = round(mean(hp), 0),
.groups = 'drop'
)

DT::datatable(gear_summary,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```

################################################################################

# 🌸 Flowers Analysis {.tabset .tabset-fade}

## Petal Patterns

### 🌼 Petal Length Distribution {data-width=500}

```{r}
p4 <- ggplot(iris, aes(x = Petal.Length, fill = Species)) +
geom_histogram(alpha = 0.7, bins = 20, color = "white") +
facet_wrap(~Species, scales = "free_y", ncol = 2) +
labs(title = "Petal Length by Species",
x = "Petal Length (cm)", y = "Count") +
theme_minimal(base_size = 11) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 12))

ggplotly(p4, height = 350)

```


### 📏 Iris Measurements {data-width=500}

```{r}
iris_summary <- iris %>%
group_by(Species) %>%
summarise(
Count = n(),
Petal_Mean = round(mean(Petal.Length), 2),
Sepal_Mean = round(mean(Sepal.Length), 2),
Petal_Ratio = round(mean(Petal_Ratio), 2),
.groups = 'drop'
)

DT::datatable(iris_summary,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```

## Species Comparison

### 🌺 Sepal vs Petal Scatter {data-width=500}

```{r}
p5 <- ggplot(iris, aes(x = Sepal.Length, y = Petal.Length,
color = Species, text = paste("Ratio:", round(Petal_Ratio, 2)))) +
geom_point(size = 4, alpha = 0.8) +
geom_smooth(method = "lm", se = TRUE, alpha = 0.3) +
labs(title = "Sepal vs Petal Length by Species",
x = "Sepal Length (cm)", y = "Petal Length (cm)") +
theme_minimal(base_size = 11) +
theme(plot.title = element_text(hjust = 0.5, size = 12),
legend.position = "bottom")

ggplotly(p5, tooltip = "text", height = 350)

```

### 📊 Ratio Analysis {data-width=500}

```{r}
ratio_summary <- iris %>%
group_by(Species) %>%
summarise(
Petal_Ratio = round(mean(Petal_Ratio), 2),
Sepal_Ratio = round(mean(Sepal_Ratio), 2),
SD_Petal = round(sd(Petal_Ratio), 2),
.groups = 'drop'
)

DT::datatable(ratio_summary,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```


## Size Categories

### 📐 Petal Size Matrix {data-width=500}

```{r}
p6 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width,
color = Species, size = Petal_Ratio)) +
geom_point(alpha = 0.7) +
labs(title = "Petal Dimensions by Species",
x = "Petal Length (cm)", y = "Petal Width (cm)") +
theme_minimal(base_size = 11) +
theme(plot.title = element_text(hjust = 0.5, size = 12))

ggplotly(p6, height = 350)
```


### 🔍 Size Statistics {data-width=500}

```{r}
size_stats <- iris %>%
mutate(Size_Group = ifelse(Petal.Length > 4, "Large", "Small")) %>%
group_by(Species, Size_Group) %>%
summarise(Count = n(), .groups = 'drop')

DT::datatable(size_stats,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```

################################################################################

# 🌿 Plants Analysis {.tabset .tabset-fade}

## Treatment Effects

### 📈 Plant Weight Comparison {data-width=500}

```{r}
p7 <- ggplot(PlantGrowth, aes(x = group, y = weight, fill = Weight_Category)) +
geom_boxplot(alpha = 0.7) +
geom_jitter(width = 0.2, alpha = 0.8, size = 3) +
labs(title = "Plant Growth by Treatment",
x = "Treatment Group", y = "Dry Weight (g)") +
theme_minimal(base_size = 11) +
theme(plot.title = element_text(hjust = 0.5, size = 12))

ggplotly(p7, height = 350)
```

### 📋 Treatment Summary {data-width=500}

```{r}
plant_summary <- PlantGrowth %>%
group_by(group, Weight_Category) %>%
summarise(
Count = n(),
Mean_Weight = round(mean(weight), 2),
SD = round(sd(weight), 2),
.groups = 'drop'
)

DT::datatable(plant_summary,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```


## Weight Distribution

### 📊 Density by Treatment {data-width=500}

```{r}
p8 <- ggplot(PlantGrowth, aes(x = weight, fill = group)) +
geom_density(alpha = 0.7) +
facet_wrap(~group, ncol = 2) +
labs(title = "Weight Density by Treatment",
x = "Dry Weight (g)", y = "Density") +
theme_minimal(base_size = 11) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 12))

ggplotly(p8, height = 350)
```


### 🔢 Detailed Statistics {data-width=500}

```{r}
plant_stats <- PlantGrowth %>%
group_by(group) %>%
summarise(
N = n(),
Mean = round(mean(weight), 2),
Median = round(median(weight), 2),
SD = round(sd(weight), 2),
Min = round(min(weight), 2),
Max = round(max(weight), 2),
.groups = 'drop'
)

DT::datatable(plant_stats,
options = list(
pageLength = 5,
scrollY = "200px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```


## Growth Categories

### 🎯 Weight Category Analysis {data-width=500}

```{r}
p9 <- ggplot(PlantGrowth, aes(x = group, fill = Weight_Category)) +
geom_bar(position = "fill", alpha = 0.8) +
labs(title = "Proportion of High/Low Weight Plants",
x = "Treatment Group", y = "Proportion") +
theme_minimal(base_size = 11) +
theme(plot.title = element_text(hjust = 0.5, size = 12),
legend.position = "bottom")

ggplotly(p9, height = 350)
```


### 📊 Category Breakdown {data-width=500}

```{r}
category_breakdown <- PlantGrowth %>%
group_by(group, Weight_Category) %>%
summarise(Count = n(), Percentage = round(n()/nrow(PlantGrowth)*100, 1),
.groups = 'drop')

DT::datatable(category_breakdown,
options = list(
pageLength = 5,
scrollY = "180px",
scrollCollapse = TRUE,
autoWidth = TRUE
),
rownames = FALSE,
fillContainer = TRUE,
class = 'compact')
```