Skip to content

Data visualization

This page covers data visualization using different programming tools, including R, Stata, and Python.

Related pages:

Resources

Style guides

Documentation

  • TBD
  • TBD
  • TBD

Community guides

Cheatsheets

General discussions

Compilation of resources:

  • TBD

Templates

Graphs made from one dataset

ggplot(data = DATA) +

  labs(
    x = "[X_AXIS_LABEL]",
    y = "[Y_AXIS_LABEL]",
    # title = "[PLOT_TITLE]",
  ) +
  theme_minimal()

Figure style guides

Remove the clutter

TBD

Languages and packages

ggplot2

R
ggplot(data = DATA) +
    GEOM_FUNCTION() +
    COORDINATE_FUNCTION() +
    FACET_FUNCTION() +
    SCALE_FUNCTION() +
    ANNOTATION_FUNCTION() +
    THEME_FUNCTION()

Summary

Types of graphs

Bar graphs

geom_col(
  aes(x = VAR_X, y = VAR_Y), 
  fill = "grey30"
) +

TBD

TBD

Line graphs

Graphs with intervals

Graphs with intervals are great for visualizing confidence intervals, among other things.

A rule of thumb: error cap \(\approx\) \(20--25\%\) of bar width.

Add a verticle / horizontal line

geom_hline(yintercept = VALUE, linetype = "dashed", color = "black") +

TBD

TBD

Scales

y- or x-axis

Percent

scale_y_continuous(
  labels = function(x) paste0(x, "%"), 
  limits = c(0, 100), breaks = seq(0, 100, 25)
) +

Data Aesthetics

Color (color / fill)

In the terminology of ggplot2, color refers to both the color and fill aesthetics.

scale_fill_manual(
  values = c("Democrat" = "#2E5EAA", "Republican" = "#C93135"),
  name = "Participant\nParty"
) +

Transparency

TBD

Linetype

TBD

Linewidth

TBD

Shape

TBD

Size

TBD

Other graph elements

Grid

Put inside `theme()`
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),

Axis title

Put inside `theme()`
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),

Legend

Put inside `theme()`
legend.position = "bottom",
legend.position = "none",

Manipulating graphs

Faceting

facet_wrap()

Put graphs together

In R, we can use the patchwork package to combine several plots into a single figure.

TBD

Saving and storing a graph

For reproducibility, we can specify as many ambiguously determined parameters as possible.

Graph size

Full width:

  • 6.5 \(\times\) 3.5 in

Middle ground:

  • 6 \(\times\) 3.5 in

  • 6 \(\times\) 3.8 in

Compact:

  • 4.8 \(\times\) 3.2 in