This project helped me explore interactive data visualization using
Plotly and Flexdashboard. I learned how to enhance ggplot graphics by
making them interactive with ggplotly() and how to
structure a simple dashboard using the flexdashboard
package. The interactive elements make it easier to interpret complex
datasets and trends by enabling dynamic tooltips, zooming, and
filtering. It was fun experimenting with the gapminder
dataset and seeing life expectancy and GDP trends over time and across
continents.
library(tidyverse)
library(plotly)
library(gapminder)
# Load and filter data
gapminder_2007 <- gapminder %>% filter(year == 2007)
gg <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, size = pop, color = continent,
text = paste("Country:", country,
"\nGDP per Capita:", round(gdpPercap),
"\nLife Expectancy:", lifeExp,
"\nPopulation:", format(pop, big.mark=",")))) +
geom_point(alpha = 0.7) +
scale_x_log10() +
labs(title = "Life Expectancy vs GDP per Capita (2007)",
x = "GDP per Capita (log scale)",
y = "Life Expectancy") +
theme_minimal()
ggplotly(gg, tooltip = "text")
countries_to_plot <- c("United States", "India", "China", "Brazil")
gg2 <- ggplot(gapminder %>% filter(country %in% countries_to_plot),
aes(x = year, y = lifeExp, color = country,
text = paste("Country:", country, "\nYear:", year, "\nLife Expectancy:", lifeExp))) +
geom_line() +
geom_point() +
labs(title = "Life Expectancy Over Time",
x = "Year",
y = "Life Expectancy") +
theme_minimal()
ggplotly(gg2, tooltip = "text")
pop_plot <- gapminder %>%
filter(country %in% countries_to_plot) %>%
ggplot(aes(x = year, y = pop, color = country,
text = paste("Country:", country,
"\nYear:", year,
"\nPopulation:", format(pop, big.mark=",")))) +
geom_line(alpha = 0.7) +
geom_point(size = 2, alpha = 0.7) +
labs(title = "Population Growth Over Time",
x = "Year",
y = "Population") +
scale_y_continuous(labels = scales::comma) +
theme_minimal()
ggplotly(pop_plot, tooltip = "text")
Install the {flexdashboard} package and create a new R Markdown file in your project by going to File > New File… > R Markdown… > From Template > Flexdashboard.
Using the documentation for {flexdashboard} online, create a basic dashboard that shows a plot (static or interactive) in at least three chart areas. Play with the layout if you’re feeling brave.