Tweet me @itschekkers!

Introduction

The nomogrammer function is a simple visualization function to plot Fagan’s nomograms as ggplot2 objects.

They are used to illustrate the change from pre-test to post-test probabilities that occur when a predictive test is used.

The underlying calculation is as follows:

Rationale

The Fagan nomogram is a graphical tool for estimating how much the result on a diagnostic test changes the probability that a patient has a disease (NEJM 1975; 293: 257). Although there are R packages for creating more complicated nomograms (e.g. the rms package, and web-implementations for creating simple Fagan nomograms, there is no simple nomogram plotting function in R.

The nomogrammer function offers such a plotting method, using the “grammar of graphics” implemented in the ggplot2 package to render the plot. Its output is designed to be close to that which is normally obtained through online nomogram builders.

Installation

nomogrammer is a standalone function:

source("https://raw.githubusercontent.com/achekroud/nomogrammer/master/nomogrammer.r")

Dependencies

There are two main package dependencies for nomogrammer: ggplot2 and scales

library(ggplot2)
library(scales)

The ggplot2 package can be installed from CRAN through install.packages. Do the same to install the scales package, i.e. install.packages("scales")

Basic Example

The function always requires the prevalence (i.e. prior probability), expressed as a probability (not a percentage).

You then either give it the positive and negative likelihood ratios:

nomogrammer(Prevalence = .60,
            Plr = 12,
            Nlr = 0.6)

Or the sensitivity and specificity of the model.

nomogrammer(Prevalence = .60,
            Sens = 0.421,
            Spec = 0.965)

And it will plot the basic nomogram

The nomogram is returned as a ggplot object, which you could save and plot as usual

p <- nomogrammer(Prevalence = .60, Sens = 0.421, Spec = 0.965)
plot(p)

Options

There are a couple of optional extras with this function.

You can use the NullLine argument to overlay a line that goes from your prior probability through LR = 1, illustrating the posterior probability if it remained unchanged.

You can use the Detail argument to overlay some helpful metrics in the top right corner.

Visual tweaks using ggplot methods

Since nomogrammer returns a ggplot object, it is easy to edit with usual methods

You can add a title, for example

p <- nomogrammer(Prevalence = .60, Sens = 0.421, Spec = 0.965)
p + ggtitle("put your title here", subtitle = "and your subtitle here")

You can change the colours of the lines, either the lazy way (not run)

## Not run
nomogrammer(Prevalence = .60, Plr = 120, Nlr = 0.4) +
    scale_color_manual(values=c("pink", "black"))

Or the precise way (hex codes):

nomogrammer(Prevalence = .60, Plr = 120, Nlr = 0.4) +
    scale_color_manual(values=c("#ff7f00", "#b2df8a"))

In general, further control over plot is possible through any theme and guides methods that also apply:

Saving

Saving the plot is also easy using the ggsave function (here I picked some size settings that looked reasonable):

p <- nomogrammer(Prevalence = .60, Sens = 0.421, Spec = 0.965)

ggsave(p, file = "~/nomogrammer/plot.pdf",
       width = 5.99,
       height = 3.99, units = "in")

And, of course, you can save the plot however you like. Here it will save as a png instead:

demo <- nomogrammer(Prevalence = .60, Sens = 0.66, Spec=0.77) +
            ggtitle("Fagan's nomogram",
                    subtitle = "Example nomogram made with this function")

ggsave(demo, file = "~/nomogrammer/demo.png",
       width = 5.99,
       height = 3.99, units = "in")

PS You can also use the Verbose argument if, for whatever reason, you want the function to return the main statistics to the console.

Note: this doesn’t work well in Rmd, so isn’t run here.

# Not run:
# > nomogrammer(Prevalence = .60, Sens = 0.66, Spec=0.77, Verbose = TRUE)

# Returns:
# prevalence = 60%
# PLR = 2.87
# NLR = 0.442
# posterior probability (positive) = 81.1%
# posterior probability (negative) = 39.8%
# sensitivity = 66%
# specificity = 77%

Known limitations

nomogrammer is strictly limited to plotting Fagan’s nomograms quickly: it cannot plot complex nomograms that visualize multiple covariates, or non-linearities. Other packages exist for this (e.g. pynomo, rms, or hdnom)

If you find other limitations or would like to contribute improvements or corrections to nomogrammer, please feel free to submit a pull request or submit an issue, thanks!

Tweet me @itschekkers!