Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: a1e204bea5
Fetching contributors…

Cannot retrieve contributors at this time

345 lines (297 sloc) 10.335 kB
% some bold math symbosl
\title{Mathematical Biostatistics Bootcamp: Lecture 11, Plotting}
\author{Brian Caffo}
\institute[Department of Biostatistics]{
Department of Biostatistics \\
Johns Hopkins Bloomberg School of Public Health\\
Johns Hopkins University
\section{Table of contents}
\frametitle{Table of contents}
\item Histograms display a sample estimate of the density or mass
function by plotting a bar graph of the frequency or proportion of
times that a variable takes specific values, or a range of values
for continuous data, within a sample
\item The data set \texttt{islands} in the R package \texttt{datasets} contains
the areas of all land masses in thousands of square miles
\item Load the data set with the command \texttt{data(islands)}
\item View the data by typing \texttt{islands}
\item Create a histogram with the command \texttt{hist(islands)}
\item Do \texttt{?hist} for options
\begin{frame}\frametitle{Pros and cons}
\item Histograms are useful and easy, apply to continuous, discrete and even
unordered data
\item They use a lot of ink and space to display very little information
\item It's difficult to display several at the same time for comparisons
Also, for this data it's probably preferable to consider log base 10
(orders of magnitude), since the raw histogram simply says that most
islands are small
\section{Stem and leaf}
\begin{frame}\frametitle{Stem-and-leaf plots}
\item Stem-and-leaf plots are extremely useful for getting
distribution information on the fly
\item Read the text about creating them
\item They display the complete data set and so waste very little ink
\item Two data sets' stem and leaf plots can be shown back-to-back for
\item Created by John Tukey, a leading figure in the development of
the statistical sciences and signal processing
> stem(log10(islands))
The decimal point is at the |
1 | 1111112222233444
1 | 5555556666667899999
2 | 3344
2 | 59
3 |
3 | 5678
4 | 012
\item Dotcharts simply display a data set, one point per dot
\item Ordering of the of the dots and labeling of the axes
can the display additional information
\item Dotcharts show a complete data set and so have high
data density
\item May be impossible to construct/difficult to interpret
for data sets with lots of points
\item Maybe ordering alphabetically isn't the best thing for this data set
\item Perhaps grouped by continent, then nations by geography
(grouping Pacific islands together)?
\begin{frame}\frametitle{Dotplots comparing grouped data}
\item For data sets in groups, you often want to display density
information by group
\item If the size of the data permits, it displaying the whole data
is preferable
\item Add horizontal lines to depict means, medians
\item Add vertical lines to depict variation, show confidence intervals
interquartile ranges
\item Jitter the points to avoid overplotting (\texttt{jitter})
\item The InsectSprays dataset contains counts of insect deaths by
insecticide type (A, B, C, D, E, F)
\item You can obtain the data set with the command
The gist of the code is below
plot(c(.5, 6.5), range(count))
sprayTypes <- unique(spray)
for (i in 1 : length(sprayTypes)){
y <- count[spray == sprayTypes[i]]
n <- sum(spray == sprayTypes[i])
points(jitter(rep(i, n), amount = .1), y)
lines(i + c(.12, .28), rep(mean(y), 2), lwd = 3)
lines(rep(i + .2, 2),
mean(y) + c(-1.96, 1.96) * sd(y) / sqrt(n)
\item Boxplots are useful for the same sort of display as the dot
chart, but in instances where displaying the whole data set is
not possible
\item Centerline of the boxes represents the median while the
box edges correspond to the quartiles
\item Whiskers extend out to a constant times the IQR or the max value
\item Sometimes potential outliers are denoted by points beyond the whiskers
\item Also invented by Tukey
\item Skewness indicated by centerline being near one of the box edges
\begin{frame}[fragile]\frametitle{Boxplots discussion}
\item Don't use boxplots for small numbers of observations, just plot the data!
\item Try logging if some of the boxes are too squished relative to other ones;
you can convert the axis to unlogged units (though they will not be equally spaced
\item For data with lots and lots of observations omit the outliers
plotting if you get so many of them that you cant see the points
\item Example of a bad box plot
boxplot(rt(500, 2))
\begin{frame}\frametitle{Kernel density estimates}
\item Kernel density estimates are essentially more modern
versions of histograms providing density estimates for
continuous data
\item Observations are weighted according to a ``kernel'',
in most cases a Gaussian density
\item ``Bandwidth'' of the kernel effectively plays the role of
the bin size for the histogram
\item Too low of a bandwidth yields a too variable (jagged) measure
of the density
\item Too high of a bandwidth oversmooths
\item The R function \texttt{density} can be used to create KDEs
Data is the waiting and eruption times in minutes between
eruptions of the Old Faithful Geyser in Yellowstone National park
d <- density(faithful$eruptions, bw = "sj")
\begin{frame}\frametitle{Imaging example}
\item Consider the following image slice (created in R) from a high resolution MRI of a brain
\item This is a single (axial) slice of a three-dimensional image
\item Consider discarding the location information and plotting a KDE of the intensities
\item QQ-plots (for quantile-quantile) are extremely useful
for comparing data to a theoretical distribution
\item Plot the empirical quantiles against theoretical quantiles
\item Most useful for diagnosing normality
\item Let $x_p$ be the $p^{th}$ quantile from a $N(\mu, \sigma^2)$
\item Then $P(X \leq x_p) = p$
\item Clearly $P(Z \leq \frac{x_p - \mu}{\sigma}) = p$
\item Therefore $x_p = \mu + z_p \sigma$ (this should not be news)
\item Result, quantiles from a $N(\mu,\sigma^2)$ population should
be linearly related to standard normal quantiles
\item A normal qq-plot plot the empirical quantiles against the
theoretical standard normal quantiles
\item In R \texttt{qqnorm} for a normal QQ-plot and \texttt{qqplot}
for a qqplot against an arbitrary distribution
\section{Mosaic plots}
\begin{frame}[fragile]\frametitle{Mosaic plots}
\item Mosaic plots are useful for displaying contingency table data
\item Consider Fisher's data regarding hair and eye color data for people from Caithness
mosaicplot(caith, color = topo.colors(4),
main = "Mosiac plot")
fair red medium dark black
blue 326 38 241 110 3
light 688 116 584 188 4
medium 343 84 909 412 26
dark 98 48 403 681 85
Jump to Line
Something went wrong with that request. Please try again.