Making effect size plots for t-test results

  • I always try to emphasize making effect size plots.
  • In a publication I would probably not devout an entire plot just to the effect size of a t-test, but I think making a simple graph like this is good practice.
  • Here I go through the steps I would do to make a polished plot

Functions and arguements used

  • t.test
  • gplots::plotCI()
  • par(xaxt=‘n’): removes axis ticks and numbers’
  • par(mar = c(x,y,z,t)): change size of margins (bottom, left, top, right)
  • mtext: adds texts to margins; can be adjusted to show up anywhere
  • abline
  • col: change color of a line
  • lty: change line type

Data: Fisher’s cat data

library(MASS)
data(cats)
summary(cats)
##  Sex         Bwt             Hwt       
##  F:47   Min.   :2.000   Min.   : 6.30  
##  M:97   1st Qu.:2.300   1st Qu.: 8.95  
##         Median :2.700   Median :10.10  
##         Mean   :2.724   Mean   :10.63  
##         3rd Qu.:3.025   3rd Qu.:12.12  
##         Max.   :3.900   Max.   :20.50

t-test: are male cats bigger

t.test(Bwt ~ Sex, data = cats)
## 
##  Welch Two Sample t-test
## 
## data:  Bwt by Sex
## t = -8.7095, df = 136.84, p-value = 8.831e-15
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.6631268 -0.4177242
## sample estimates:
## mean in group F mean in group M 
##        2.359574        2.900000
  • Yup



Make and effect size plot of this t-test

Save t-test output

t.test.out <- t.test(Bwt ~ Sex, data = cats)



Calculate difference of means

  • This is our effect size
means <- t.test.out$estimate

diff <- means[1]-means[2]



Get CI of difference

  • use $ to access the “conf.int” part of the list of t.test output
CI <- t.test.out$conf.int



Plot effect size

Default plot

Use gplots::plotCI()

library(gplots)
## 
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
## 
##     lowess
plotCI(x = diff,
       li = CI[1],
       ui = CI[2])



Get rid of ticks & tick lables

  • Use par(xaxt=‘f’)
par(xaxt='n')
plotCI(x = diff,
       li = CI[1],
       ui = CI[2])



  • Once you set something in par() every subsequent plot will have those settings
  • To go back to the default you have to rest “xaxt = ‘t’”
par(xaxt='t')
plotCI(x = diff,
       li = CI[1],
       ui = CI[2])

Label y axis

  • Put the default label on the y axis
par(xaxt='n')
plotCI(x = diff,
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "Effect Size")

Add annotation using text()

  • Not pos = 4 in text() sets it so that the text appears to the right of the point you specify with the x and y coords
  • The default is to center the text and it disappears out of the plotting areas
  • This is annoying
par(xaxt='n')
plotCI(x = diff,
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "Effect Size")
text(x = 0.56, y = -0.45,
     pos = 4,
     label = "Error bars = +/- 95% CI")

Add annotation using mtext()

  • note: Instead of text() you can use mtext() to place annotations
  • mtext = “margin text”
  • note that instead of “label =” for the text its “text =”
  • this is annoying
  • mtext() is nice b/c you don’t have to specify exact x and y coordinates
  • i.e, It can automatically center, right, or left justify on it own.
  • side = 3 sets it to be on the top
  • adj = 0 set it to be left justified
  • line = -1 sets it below the top of the box outlining the plot
#set par() in case not already set
par(xaxt='n')

#The plot
plotCI(x = diff,
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "Effect Size")

#The label
mtext(text = "Error bars = +/- 95% CI",
      side = 3, 
      adj = 0,
      line = -1)



Remove excess margin space

  • Use par(mar = c(…))
#set margins
par(mar = c(1,4,1,1))

#plot
plotCI(x = diff,
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "Effect Size")

#label
mtext(text = "Error bars = +/- 95% CI",
      side = 3, 
      adj = 0,
      line = -1)



Move y axis label closer to the axis & get rid of excess margin

  • Use part(mar = …) to adjust the margin
  • Use mtext() for the new axis label that is closer to the axis
#Set part
par(mar = c(1,3,1,1))

#plot
plotCI(x = diff,
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "") #get rid of ylab

#new label for y axis
mtext(text = "Effect size", side = 2, line = 2)

#annotation about error bars
mtext(text = "Error bars = +/- 95% CI",
      side = 3, 
      adj = 0,
      line = -1)

Adjust y axis so that zero is in plot

  • 0.0 is what the effect size is being compared against.
  • set ylim = …
par(mar = c(1,3,1,1))
plotCI(x = diff,
       ylim = c(-0.75,0.1),
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "") #get rid of ylab

mtext(text = "Effect size", side = 2, line = 2)

#annotation about error bars
mtext(text = "Error bars = +/- 95% CI",
      side = 3, 
      adj = 0,
      line = -1)



Add referene line at 0.0

  • abline(h = ) for a horizontal line
  • lty = changes the type of line (solid, dashed etc)
  • col = changes the colr
par(mar = c(1,3,1,1))
plotCI(x = diff,
       ylim = c(-0.75,0.1),
       li = CI[1],
       ui = CI[2],
       xlab = "",
       ylab = "") #get rid of ylab

#add reference line
abline(h = 0, col = 2, lty = 2)

# y axis label
mtext(text = "Effect size", side = 2, line = 2)

#annotation about error bars
mtext(text = "Error bars = +/- 95% CI",
      side = 3, 
      adj = 0,
      line = -1)