pairedDiffHist: Add histogram of paired difference on the identity line

Description Usage Arguments Details Author(s) See Also Examples

Description

pairedDiffHist Adds a histogram on the identity line which illustrates the distribution of the difference between two conditions. This is a useful addition to a paired scatterplot, to illustrate how the conditions differ within the sample. Each histogram bin is manually added to the plot with geom_polygon.

Usage

1
2
3
4
5
pairedDiffHist(p, origin, maxheight, colour = "black", fill = "grey",
  size = 1, bins = NULL, binwidth = NULL, meanline = FALSE,
  meanline_pos = 0, meanline_colour = "black", meanline_size = 1,
  meanline_type = "solid", sigstars = NULL, sigstars_pos = 0,
  sigstars_colour = "black", sigstars_size = 1)

Arguments

p

the ggplot object to which the histogram should be added.

origin

number indicating the center of the histogram on the plot.

maxheight

number indicating the desired height of the tallest bin.

colour, fill, size

aesthetics for the histogram bins.

bins, binwidth

the number of bins / the width of the bins.

meanline

logical. Should a line segment at the mean of the histogram be added?

meanline_pos

number indicating by how much the line should extend the dimensions of the biggest histogram bin.

meanline_colour, meanline_size, meanline_type

aesthetics of the mean line.

sigstars

number indicating the amount of stars to be added at the mean of the distribution, to indicate significance level of difference. Default is NULL.

sigstars_pos

number indicating by how much the significance stars should be shifted up from the top of the mean line

sigstars_colour, sigstars_size

aesthetics of the significance stars.

Details

In order for the histogram to make sense, the axes of the plot should be identically scaled. Ideally the aspect ratio is set to one, using theme(aspect.ratio = 1).

Author(s)

Frank H. Hezemans, Frank.Hezemans@mrc-cbu.cam.ac.uk

See Also

geom_histogram, geom_polygon, annotate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
library(ggplot2)
library(UsingR) # Package contains dataset

# Get data
data <- crime

# Create paired scatterplot
paired_scatter <- ggplot(data = data, aes(x = y1983, y = y1993)) +
    geom_abline(slope = 1, intercept = 0) +
    geom_point(shape = 21, fill = "grey") +
    xlim(0, 1500) + ylim(0, 1500) +
    labs(title = "Violent crime in 50 U.S. states in 1983 and 1993",
         subtitle = "Rates per 100,000 population",
         x = "Violent crime rate in 1983",
         y = "Violent crime rate in 1993") +
    theme_minimal() +
    theme(aspect.ratio = 1)

# Add histogram of paired differences
paired_scatter <- pairedDiffHist(paired_scatter, origin = 1250, maxheight = 100,
                                 colour = "black", fill = "grey", size = 1,
                                 bins = 6, meanline = TRUE, sigstars = 3,
                                 sigstars_pos = 25)

frankhezemans/fhTools documentation built on May 7, 2019, 3:14 p.m.