# Load necessary libraries
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(gapminder)
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
# Prepare line chart: Life Expectancy Trend in Asia
line_chart <- gapminder %>%
  filter(continent == "Asia") %>%
  plot_ly(
    x = ~year,
    y = ~lifeExp,
    color = ~country,
    type = "scatter",
    mode = "lines"
  ) %>%
  layout(title = "Life Expectancy Trend in Asia")

# Prepare scatter plot: GDP vs Life Expectancy, sized by population, colored by continent
scatter_plot <- gapminder %>%
  plot_ly(
    x = ~gdpPercap,
    y = ~lifeExp,
    color = ~continent,
    size = ~pop,
    type = "scatter",
    mode = "markers",
    hoverinfo = "text",
    text = ~paste(
      "Country:", country, "<br>",
      "GDP per Capita:", round(gdpPercap, 2), "<br>",
      "Life Expectancy:", lifeExp
    )
  ) %>%
  layout(title = "GDP vs Life Expectancy by Continent")

# Prepare bar chart: Life Expectancy by Country for year 2007
bar_chart <- gapminder %>%
  filter(year == 2007) %>%
  plot_ly(
    x = ~country,
    y = ~lifeExp,
    type = "bar",
    hoverinfo = "text",
    text = ~paste(
      "Country:", country, "<br>",
      "Life Expectancy:", lifeExp
    )
  ) %>%
  layout(title = "Life Expectancy by Country (2007)")

# Combine the three plots into an interactive dashboard (1 row)
dashboard <- subplot(
  scatter_plot, bar_chart, line_chart,
  nrows = 1,
  margin = 0.05,
  titleX = TRUE,
  titleY = TRUE
) %>%
  layout(title = "Interactive Gapminder Dashboard")
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning: `line.width` does not currently support multiple values.
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
# Display the dashboard
dashboard