Interactive Dashboard

Exercise 11 — PMAP 8551, Summer 2024

Author

Vishakha

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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
library(plotly)
Warning: package 'plotly' was built under R version 4.4.1

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(scales)

Attaching package: 'scales'

The following object is masked from 'package:purrr':

    discard

The following object is masked from 'package:readr':

    col_factor
# Load data
data <- gapminder

#Column {data-width=600}

###Chart 1: Life Expectancy vs GDP per Capita

# Create scatter plot
static_scatter_plot <- ggplot(data, aes(x = gdpPercap, y = lifeExp, color = continent, text = country)) +
  geom_point() +
  scale_x_log10(labels = scales::dollar_format()) +
  labs(title = "Life Expectancy vs GDP per Capita",
       x = "GDP per Capita (log scale)",
       y = "Life Expectancy",
       color = "Continent",
       caption = "Source: Gapminder") +
  theme_minimal()

# Make it interactive
interactive_scatter_plot <- ggplotly(static_scatter_plot, tooltip = c("text", "x", "y"))

# Print interactive plot
interactive_scatter_plot

#Column {data-width=400}

###Chart2: Average Life Expectancy by Continent

# Create bar plot
bar_plot <- ggplot(data %>% group_by(continent) %>% summarize(avg_lifeExp = mean(lifeExp)), 
            aes(x = continent, y = avg_lifeExp, fill = continent, text = paste("Avg Life Exp:", round(avg_lifeExp, 1)))) +
  geom_bar(stat = "identity") +
  labs(title = "Average Life Expectancy by Continent",
       x = "Continent",
       y = "Average Life Expectancy",
       fill = "Continent") +
  theme_minimal()

# Make it interactive
interactive_bar_plot <- ggplotly(bar_plot, tooltip = c("text", "y"))

# Print interactive bar plot
interactive_bar_plot

###Chart 3: Population over Time

# Create line plot
line_plot <- ggplot(data, aes(x = year, y = pop, group = country, color = continent, text = country)) +
  geom_line(alpha = 0.5) +
  labs(title = "Population Over Time",
       x = "Year",
       y = "Population",
       color = "Continent") +
  scale_y_continuous(labels = scales::comma) +
  theme_minimal()

# Make it interactive
interactive_line_plot <- ggplotly(line_plot, tooltip = c("text", "y"))

# Print interactive line plot
interactive_line_plot