library(tidyverse)
library(plotly)
library(scales)
# Load data here
wdi_parliament <- read_csv("wdi_parliament.csv")Interactivity
Exercise 11 — PMAP 8551, Fall 2024
Task 1: Reflection
There are three options for how to make interactive plots. First, you can create a single plot with plotly by creating a graph and then doing ggplotly(graph_name). This will make the plot interactive where you can hover over points and see details about them. You can also filter which points to be shown as well as zoom in. The second way to make an interactive plot is with flexdashboard. This option allows you to create an interactive dashboard with multiple plots. The third option is interactive apps with Shiny but it is the most complicated and difficult to learn. Once the interactive plot has been created, the best option for sharing is to publish it on RPubs and share the URL for individuals to access it. But this is only an option when the data is not confidential because RPubs is open to the public.
Task 2: Interactive plots
Do the following:
- Make a plot. Any kind of plot will do (though it might be easiest to work with
geom_point()).
wdi_2019 <- wdi_parliament |>
filter(region != "Aggregates") |>
filter(year == 2019) |>
drop_na(prop_women_parl) |>
mutate(prop_women_parl = prop_women_parl / 100)static_plot <- ggplot(wdi_2019,
aes(x = prop_women_parl, y = gdp_per_cap, color = region)) +
geom_point() +
scale_x_continuous(labels = label_percent()) +
labs(x = "% women in parliament", y = "GDP per capita", caption = "Source: The World Bank") +
theme_bw()
static_plotWarning: Removed 5 rows containing missing values or values outside the scale range
(`geom_point()`).
- Make the plot interactive with
ggplotly().
ggplotly(static_plot)- Make sure the hovering tooltip is more informative than the default.
static_plot_toolip <- ggplot(wdi_2019,
aes(x = prop_women_parl, y = gdp_per_cap, color = region)) +
geom_point(aes(text = country),
position = position_jitter(width = 0, height = 0.15, seed = 1234)) +
scale_x_continuous(labels = label_percent()) +
labs(x = "% women in parliament", y = "GDP per capita", caption = "Source: The World Bank") +
theme_bw()Warning in geom_point(aes(text = country), position = position_jitter(width =
0, : Ignoring unknown aesthetics: text
ggplotly(static_plot_toolip)Good luck and have fun!
Task 3:
Create a Quarto dashboard in a new .qmd file that shows a plot (static or interactive) in at least three chart areas. See the example for this session for resources on creating dashboards in Quarto.