Which Visualization Should I Use?

September 22, 2024

Line Charts


  • Line charts are used to show trends over time
  • You especially want to use a line chart when you have multiple cases or categories that you want to compare over time

Huntington’s Three Waves


Line Chart Example



Here is the code…


# in this ggplot() call, we add a third dimension for line color
ggplot(dem_waves_ctrs, aes(x = year, y = polyarchy, color = country)) +
  geom_line(linewidth = 1) + # our geom is a line with a width of 1
  labs(
    x = "Year", 
    y = "Polyarchy Score", 
    title = 'Democracy in countries representing three different "waves"', 
    caption = "Source: V-Dem Institute", 
    color = "Country" # make title of legend to upper case
  )


Use geom_line() to specify a line chart…


# in this ggplot() call, we add a third dimension for line color
ggplot(dem_waves_ctrs, aes(x = year, y = polyarchy, color = country)) +
  geom_line(linewidth = 1) + # our geom is a line with a width of 1
  labs(
    x = "Year", 
    y = "Polyarchy Score", 
    title = 'Democracy in countries representing three different "waves"', 
    caption = "Source: V-Dem Institute", 
    color = "Country" # make title of legend to upper case
  )


Add third dimension to the aes() call for line color…


# in this ggplot() call, we add a third dimension for line color
ggplot(dem_waves_ctrs, aes(x = year, y = polyarchy, color = country)) +
  geom_line(linewidth = 1) + # our geom is a line with a width of 1
  labs(
    x = "Year", 
    y = "Polyarchy Score", 
    title = 'Democracy in countries representing three different "waves"', 
    caption = "Source: V-Dem Institute", 
    color = "Country" # make title of legend to upper case
  )


Modify the legend title…


# in this ggplot() call, we add a third dimension for line color
ggplot(dem_waves_ctrs, aes(x = year, y = polyarchy, color = country)) +
  geom_line(linewidth = 1) + # our geom is a line with a width of 1
  labs(
    x = "Year", 
    y = "Polyarchy Score", 
    title = 'Democracy in countries representing three different "waves"', 
    caption = "Source: V-Dem Institute", 
    color = "Country" # make title of legend to upper case
  )

Your Turn!


  • See table three of this article
  • Select three countries to visualize
  • Adjust setup code to filter data on those countries
  • Visualize with geom_line()
10:00

Scatter Plots


  • Scatter plots are used to show the relationship between two variables
  • Frequently the outcome variable is on the y-axis and the predictor variable is on the x-axis
  • In addition to the points, you can use color, size, and shape to add more information to the plot

Scatter Plot Setup


dem_summary_ctry <- read_csv("data/dem_women.csv") |>
  group_by(country, region) |> # group by country, keep region
  summarize(
    polyarchy = mean(polyarchy, na.rm = TRUE),
    gdp_pc = mean(gdp_pc, na.rm = TRUE), 
    flfp = mean(flfp, na.rm = TRUE), 
    women_rep = mean(women_rep, na.rm = TRUE)
  )

Scatter Plot

Code
ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy, color = region, size = women_rep)) + 
  geom_point() + # use geom_point() for scatter plots
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region",
    size = "Women Reps"
    ) 

Scatter Plot


Use geom_point()

ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy, color = region, size = women_rep)) +
  geom_point() + # use geom_point() for scatter plots
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region",
    size = "Women Reps"
    ) 

Scatter Plot


Four dimensions…

ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy, color = region, size = women_rep)) + 
  geom_point() + # use geom_point() for scatter plots
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region",
    size = "Women Reps"
    ) 

Scatter Plot


Stretch axis on log scale and use scales package to adjust labels…

ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy, color = region, size = women_rep)) + 
  geom_point() + # use geom_point() for scatter plots
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region",
    size = "Women Reps"
    ) 

Scatter Plot


Change legend titles…

ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy, color = region, size = women_rep)) + 
  geom_point() + # use geom_point() for scatter plots
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region",
    size = "Women Reps"
    ) 

Your Turn!

  • There are four variables in dem_summary_ctry
  • Pick one related to women’s empowerment
  • Visualize it on the y-axis with gdp_pc or polyarchy on the x-axis
  • Change labels and legend titles to match your visualization
  • Interpret your plot
10:00

Add a Trend Line

Add a Trend Line


ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy)) + 
  geom_point(aes(color = region)) + 
  geom_smooth(method = "lm", linewidth = 1) + 
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region"
    ) 

Add a Trend Line


Taking out size and adding color to geom_point() call…

ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy)) + 
  geom_point(aes(color = region)) + 
  geom_smooth(method = "lm", linewidth = 1) + 
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region"
    ) 

Add a Trend Line


Changing legend titles…

ggplot(dem_summary_ctry, aes(x = gdp_pc, y = polyarchy)) + 
  geom_point(aes(color = region)) + 
  geom_smooth(method = "lm", linewidth = 1) + 
  scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
  labs(
    x= "GDP per Capita", 
    y = "Polyarchy Score",
    title = "Wealth and democracy, 1990 - present", 
    caption = "Source: V-Dem Institute", 
    color = "Region"
    ) 

Your Turn!


  • Add a trendline to your plot
  • Change the labels accordingly
  • Try using method = "loess" instead of a “lm”
10:00

Which Plot Should You Use?


  • Trends in stock process over time
  • Distribution of income in a country
  • Comparison of FLFP across MENA countries
  • Relationship between poverty and inequality (cross-nationally)

Which Geom Would You Use?


  • Column chart
  • Histogram
  • Line chart
  • Scatter plot

Other Plots and Geometries


  • Box Plot
    geom_boxplot()

  • Violin Plot
    geom_violin()

  • Density Plot
    geom_density()

  • Bar Plot (Categorical)
    geom_bar()

  • Heatmap
    geom_tile()

  • Area Plot
    geom_area()

  • Dot Plot
    geom_dotplot()

  • Pie Chart
    (usually a bar plot with coord_polar())

  • Ridgeline Plot
    ggridges::geom_density_ridges()

  • Map Plot (Choropleth)
    geom_polygon()