Diagram types with the ggplot2 package

Install required packages

ggplot2, ggbeeswarm, cowplot, patchwork

Simulate data

Diagram types

Scatterplot

plot of chunk ggplot_types01
plot of chunk ggplot_types01

Line plot

group=TRUE is necessary here

    group   mood
1 control  86.79
2 placebo 104.29
3  treatA 114.53
4  treatB 105.14
plot of chunk ggplot_types02a
plot of chunk ggplot_types02a
    group   mood
1 control  86.79
2 placebo 104.29
3  treatA 114.53
4  treatB 105.14
plot of chunk ggplot_types02b
plot of chunk ggplot_types02b

Barplot

Absolute frequencies

plot of chunk ggplot_types03
plot of chunk ggplot_types03

Relative frequencies

plot of chunk ggplot_types04
plot of chunk ggplot_types04

Histogram

Absolute frequencies

plot of chunk ggplot_types06
plot of chunk ggplot_types06

Density with nonparametric kernel density estimator

plot of chunk ggplot_types07
plot of chunk ggplot_types07

Boxplot

Add a geom_beeswarm() layer to show raw data.

plot of chunk ggplot_types08
plot of chunk ggplot_types08

Q-Q plot

Against normal distribution

plot of chunk ggplot_types09
plot of chunk ggplot_types09

Against pre-specified distribution (here: t)

plot of chunk ggplot_types10
plot of chunk ggplot_types10

Compare quantiles of two variables

plot of chunk ggplot_types11
plot of chunk ggplot_types11

Multi-panel plots

Split the diagram into panels by column using facet_grid().

plot of chunk ggplot_types12a
plot of chunk ggplot_types12a

Split the diagram into matrix of panels using facet_wrap(). Option scales="free_y" produces separate y-axis for each panel.

plot of chunk ggplot_types12b
plot of chunk ggplot_types12b

Additional diagram elements

Add error bars

  sex   group mood.M  mood.SD Freq     SEMlo     SEMup
1   f control  90.16 26.10916   50  86.46761  93.85239
2   f placebo 115.32 22.71064   50 112.10823 118.53177
3   f  treatA 125.52 27.40791   50 121.64394 129.39606
4   f  treatB  91.36 25.60035   50  87.73956  94.98044
5   m control  83.42 25.34826   50  79.83521  87.00479
6   m placebo  93.26 23.49452   50  89.93737  96.58263
7   m  treatA 103.54 23.58416   50 100.20470 106.87530
8   m  treatB 118.92 22.67772   50 115.71289 122.12711
plot of chunk ggplot_types13
plot of chunk ggplot_types13
  • geom_hline() for a horizontal line
  • geom_vline() for a vertical line
  • geom_smooth() for a regression line including SEs
  • geom_text() for text from a variable
  • ggtitle() for a diagram title
  • annotate() for text given directly as an argument
plot of chunk ggplot_types14
plot of chunk ggplot_types14

Put different plots on the same page

Using package cowplot

plot of chunk ggplot_types15
plot of chunk ggplot_types15

Using package patchwork

plot of chunk ggplot_types16
plot of chunk ggplot_types16
plot of chunk ggplot_types17
plot of chunk ggplot_types17

Save ggplot2 diagrams to file

Graphics file format is determined from file ending.

Further online resources

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts