Themes, Annotations and Interactivity

September 26, 2024

Column Chart from Last Class

Add a Theme

Code
flfp_region + theme_minimal()

Can Also Set Theme


If you like particular theme and want to use it for all of your visualizations, use theme_set() in a code chunk at the top of your document, e.g….


```{r}
#| label: my_setup_chunk

theme_set(theme_minimal())
```


… and R will apply that theme to all of the visualizations in your document.

Your Turn!

Produce this scatter plot… Then try different themes.

05:00

Add an Annotation

First step, add a horizontal line using geom_hline()

Code
flfp_line + 
  geom_hline(yintercept = 52, linetype="dashed", color = "red", size = 1) 

Second step, the annotation with annotate()

Code
flfp_line + 
  geom_hline(yintercept = 52, linetype="dashed", color = "red", size = 1) +
  annotate("text", x = 1995, y = 55, label = "Global average")

Vertical Reference Line

First step, add a horizontal line using geom_hline()

Code
flfp_line + 
  geom_vline(xintercept=2020, linetype = "dashed", size = 1)

Second step, add the annotation with annotate()

Code
flfp_line + 
  geom_vline(xintercept=2020, linetype = "dashed", size = 1) +
  annotate("text", x = 2017, y = 35, label = "Pandemic")

Your Turn!


  • Go to module 2.2
  • Copy the code you need to make the line chart
  • Create a horizontal reference line and annotate it
  • In a new code chunk, create a vertical line and annotate it
  • Play with the parameters to move and style your line
  • Try different text, etc.
05:00

Interactivity


The Code

library(plotly)

wealth_flfp_plotly <- wealth_flfp  + 
  scale_color_viridis_d(option = "plasma", end = .7) +
  theme_minimal() +
  aes(label = country)  # need so ggplot retains label for plotly

ggplotly(wealth_flfp_plotly, tooltip = c("country", "flfp", "gdp_pc")) |> 
  
  # add source
  layout(annotations = list(text = "Source: World Bank Development Indicators",  
                            font = list(size = 10), showarrow = FALSE,
                            xref = 'paper', x = 1.1, xanchor = 'right', xshift = 0,
                            yref = 'paper', y = -.1, yanchor = 'auto', yshift = 0)) |> 
  # add web address
  layout(annotations = list(text = "www.dataviz-gwu.rocks", 
                            font = list(size = 10, color = 'grey'), showarrow = FALSE,
                            xref = 'paper', x = .5, xanchor = 'center', xshift = 0,
                            yref = 'paper', y = 1, yanchor = 'top', yshift = 0))

The Code

library(plotly)

wealth_flfp_plotly <- wealth_flfp  + 
  scale_color_viridis_d(option = "plasma") +
  theme_minimal() +
  aes(label = country)  # need so ggplot retains label for plotly

ggplotly(wealth_flfp_plotly, tooltip = c("country", "flfp", "gdp_pc")) |> 
  
  # add source
  layout(annotations = list(text = "Source: World Bank Development Indicators",  
                            font = list(size = 10), showarrow = FALSE,
                            xref = 'paper', x = 1.1, xanchor = 'right', xshift = 0,
                            yref = 'paper', y = -.1, yanchor = 'auto', yshift = 0)) |> 
  # add web address
  layout(annotations = list(text = "www.dataviz-gwu.rocks", 
                            font = list(size = 10, color = 'grey'), showarrow = FALSE,
                            xref = 'paper', x = .5, xanchor = 'center', xshift = 0,
                            yref = 'paper', y = 1, yanchor = 'top', yshift = 0))

But to make a plotly chart, all you need is this!

Code
ggplotly(wealth_flfp_plotly)


Your Turn!

  • Go to module 2.2 and create the basic wealth vs. flfp scatter plot
  • Now “plotlify” the plot
  • At first, don’t worry about annotations or tool tip
  • Then add in tool tip, play with setting
  • Then add in the annotations
  • Move annotations, change content
05:00

Animated Plots

Animated Plots


flfp_gdp_ts <- wb_data(indicators, mrv = 25) |> # download data for 25 most recent years
    left_join(select(wb_countries(), c(iso3c, region)), by = "iso3c") |> # add regions
    rename(year = date) |>
    drop_na() # drop rows with missing values

Animated Plots


library(gganimate)

ggplot(flfp_gdp_ts, aes(x = gdp_pc, y = flfp)) + 
  geom_point(aes(color = region)) + 
  geom_smooth(method = "loess", linewidth = 1) +
  scale_x_log10(labels = scales::label_dollar()) + 
  scale_y_continuous(labels = scales::label_percent(scale = 1)) +
  transition_states(year) +
  labs(
    x= "GDP per Capita",
    y = "Female Labor Force Participation",
    title = "Wealth and female labor force participation, {closest_state}",
    caption = "Source: World Bank Development Indicators",
    color = "Region"
    ) + 
  scale_color_viridis_d(option = "plasma", end = .7) 

Your Turn!


  • Try making an animated scatter plot with different a different y-axis variable
  • See if you can make the title change based on the year displayed
05:00

Time Permitting

Annotate (comment) the following code…

wealth_flfp <- ggplot(flfp_gdp, aes(x = gdp_pc, y = flfp)) + 
  geom_point(aes(color = region)) + 
  geom_smooth(method = "loess", linewidth = 1) +
  scale_x_log10(labels = scales::label_dollar()) + 
  scale_y_continuous(labels = scales::label_percent(scale = 1)) +
  labs(
    x= "GDP per Capita",
    y = "Female Labor Force Participation",
    title = "Wealth and female labor force participation, 2021",
    caption = "Source: World Bank Development Indicators",
    color = "Region"
    ) + 
  scale_color_viridis_d(option = "plasma", end = .7) 

wealth_flfp + theme_minimal()

Time Permitting

Annotate (comment) the following code…

flfp_line <- ggplot(flfp_ts, aes(x = year, y = flfp, color = region)) +
  geom_line(linewidth = 1) + 
  scale_y_continuous(labels = scales::label_percent(scale = 1)) +
  labs(
    x = "Year", 
    y = "Female Labor Force Participation", 
    title = "Regional trends in female labor force participation", 
    caption = "Source: V-Dem Institute", 
    color = "Country"
  ) + scale_color_viridis_d() +
  theme_minimal()

flfp_line + geom_hline(yintercept = 52, linetype="dashed", color = "red", size = 1) +
  annotate("text", x = 1995, y = 55, label = "Global average")
05:00