# bland altman plot ba_ggplot <- function(m1, m2, ...) { # m1 and m2 are the measurements and the input arguments of the funtion require(ggplot2) means <- (m1 + m2) / 2 diffs <- m1 - m2 mdiff <- mean(diffs, na.rm = TRUE) sddiff <- sd(diffs, na.rm = TRUE) # Compute the figure limits ylimh <- mdiff + 3 * sddiff yliml <- mdiff - 3 * sddiff # Plot data ggplot() + scale_x_continuous("Mean measurement values") + scale_y_continuous("Differences", limits = c(yliml, ylimh)) + geom_hline(yintercept = mdiff, colour = "grey", size = 2) + geom_hline(yintercept = mdiff + 1.96 * sddiff, linetype = "dashed", colour = "grey", size = 2) + geom_hline(yintercept = mdiff - 1.96 * sddiff, linetype = "dashed", colour = "grey", size = 2) + geom_point(aes(x = means, y = diffs)) # plot this last so the grey line is under it!!! }