; PSY 1903
PSY 1903 Programming for Psychologists

Suggestion Box

Spot an error or have suggestions for improvement on these notes? Let us know!

Plot Types and Aesthetics Reference

This reference collects the most common plot types you will use in R and summarizes when each is appropriate. It also includes examples of common aesthetics and arguments for refining plots.

1. Choosing a plot type

Plot Type Function Data Type Best For
Histogram geom_histogram() Numeric Distribution
Density plot geom_density() Numeric Smooth distribution
Boxplot geom_boxplot() Numeric + Factor Comparing groups
Violin plot geom_violin() Numeric + Factor Distribution shape
Scatterplot geom_point() Numeric × Numeric Relationships
Regression line geom_smooth(method = "lm") Numeric × Numeric Trends
Bar plot (summary) geom_col() Summarized data Group means
Bar plot (counts) geom_bar() Factor Frequencies
Line plot geom_line() Ordered x Trends
Dot plot geom_dotplot() Low-range integers Likert-type
Jitter plot geom_jitter() Numeric + Factor Overplotting
Faceted plots facet_wrap(), facet_grid() Any Side-by-side views

2. Additional plot templates

These examples give us starting points for common plots that go beyond basic histograms and simple bar plots. You can copy and adapt them for your own data.

Boxplot

Boxplots summarize the distribution of a numeric variable within each group. They show the median, quartiles, and potential outliers.

ggplot(npt_data, aes(x = focus_group, y = mean_rt_overall)) +
  geom_boxplot(fill = "gray80", color = "black") +
  labs(
    title = "Distribution of Mean RT by Focus Group",
    x     = "Focus Group",
    y     = "Mean RT (ms)"
  ) +
  theme_classic()

Violin plot with jittered points

Violin plots show the shape of the distribution across groups. Adding jittered points on top lets us see individual participants as well.

ggplot(npt_data, aes(x = focus_group, y = mean_rt_overall)) +
  geom_violin(fill = "lightblue", alpha = 0.7, trim = FALSE) +
  geom_jitter(width = 0.05, alpha = 0.6, size = 2, color = "gray30") +
  labs(
    title = "Mean RT Distribution with Individual Participants",
    x     = "Focus Group",
    y     = "Mean RT (ms)"
  ) +
  theme_classic()

Density plot

Density plots give a smoothed view of a distribution. They are useful for seeing the overall shape without binning into histogram bars.

ggplot(npt_data, aes(x = mean_rt_overall)) +
  geom_density(fill = "gray70", alpha = 0.6, color = "black") +
  labs(
    title = "Density Plot of Mean Reaction Times",
    x     = "Mean RT (ms)",
    y     = "Density"
  ) +
  theme_classic()

We can also compare groups by mapping fill to a factor:

ggplot(npt_data, aes(x = mean_rt_overall, fill = focus_group)) +
  geom_density(alpha = 0.4) +
  labs(
    title = "Density of Mean RT by Focus Group",
    x     = "Mean RT (ms)",
    y     = "Density",
    fill  = "Focus Group"
  ) +
  theme_classic()

Faceted histogram

Faceting creates separate panels for each group, which can make comparisons easier than stacking everything in one plot.

ggplot(npt_data, aes(x = mean_rt_overall)) +
  geom_histogram(binwidth = 40, fill = "gray80", color = "black") +
  facet_wrap(~ focus_group) +
  labs(
    title = "Mean RT Distributions by Focus Group",
    x     = "Mean RT (ms)",
    y     = "Count"
  ) +
  theme_classic()

3. Common aesthetics and arguments

These are some of the arguments we use most often when refining and polishing plots. They can be added as extra layers to control the appearance and clarity of the figure.

Labels and titles

Clear labels help readers understand what they are looking at without needing to read a long caption.

+ labs(
    title    = "Reaction Times by Focus Group",
    subtitle = "Mean RT across NPT conditions",
    x        = "Focus Group",
    y        = "Mean RT (ms)"
  )

Colors and fills

We often want more control over the colors used for groups or conditions.

+ scale_fill_manual(
    values = c("High Focus" = "steelblue",
               "Low Focus"  = "gray50")
  )

or for color:

+ scale_color_manual(
    values = c("High Focus" = "steelblue",
               "Low Focus"  = "gray50")
  )

Axes

Control tick marks, axis ranges, and label spacing.

+ scale_y_continuous(breaks = seq(400, 800, 50))
+ scale_x_continuous(limits = c(400, 800))
+ theme(axis.text.x = element_text(angle = 45, hjust = 1))

Annotations

Annotations help point out patterns directly on the plot.

+ annotate("text",
           x     = 1.5,
           y     = 600,
           label = "High Focus slightly slower",
           color = "red")

Add a horizontal reference line:

+ geom_hline(yintercept = mean(npt_data$mean_rt_overall),
             linetype   = "dashed",
             color      = "blue")

This expanded reference is designed to provide more context and serve as a helpful toolbox as you build and refine your visualizations.