October 27, 2023
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….
… and R will apply that theme to all of the visualizations in your document.
Produce this scatter plot… Then try different themes.
10:00
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",
caption = "Source: World Bank Development Indicators",
color = "Region"
) + scale_color_viridis_d(option = "plasma", end = .7)
wealth_flfp + theme_dark()
First step, add a horizontal line using geom_hline()
…
Second step, the annotation with annotate()
…
First step, add a horizontal line using geom_hline()
…
Second step, add the annotation with annotate()
10:00
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))
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))
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))
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))
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))
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!
10:00
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",
caption = "Source: World Bank Development Indicators",
color = "Region"
) + scale_color_viridis_d(option = "plasma", end = .7)
wealth_flfp + theme_minimal()
05:00
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
Coding assignment 2 builds off this lesson
Take the remainder of the class time to work on it. I will be here in case you have questions.
You may work individually or in groups.