5

I have a table with averages and interquartile ranges. I would like to create a dotplot, where the dot would show this average, and a bar would stretch through the dot, to show the interquartile range. In other words, the dot would be at the midpoint of a bar, the length of which would equal my interquartile range data. I am working in R.

For example,

labels<-c('a','b','c','d')
averages<-c(10,40,20,30)
ranges<-c(5,8,4,10)
dotchart(averages,labels=labels)

where the ranges would then be added to this plot as bars.

Any ideas?

Thanks!

3 Answers 3

8

Yet another method, using base.

labels <- c('a', 'b', 'c', 'd')
averages <- c(10, 40, 20, 30)
ranges <- c(5, 8, 4, 10)
dotchart(averages, labels=labels, xlab='average',  pch=20,
         xlim=c(min(averages-ranges), max(averages+ranges)))
segments(averages-ranges, 1:4, averages+ranges, 1:4)

dotchart with error bars

7

For the record, here's a lattice solution, which uses a couple of functions from the Hmisc package:

library(lattice)
library(Hmisc)

labels<-c('a','b','c','d')
averages<-c(10,40,20,30)
ranges<-c(5,8,4,10)
low  <- averages - ranges/2
high <- averages + ranges/2
d <- data.frame(labels, averages, low, high)

Dotplot(labels ~ Cbind(averages, low, high), data = d, 
        col = 1,                                        # for black points
        par.settings = list(plot.line = list(col = 1)), # for black bars
        xlab = "Value")

enter image description here

4

ggplot2 has a good facility for doing this:

library(ggplot2)

labels<-c('a','b','c','d')
averages<-c(10,40,20,30)
ranges<-c(5,8,4,10)

x <- data.frame(labels,averages,ranges)

ggplot(x, aes(averages,labels)) + 
geom_point() + 
geom_errorbarh(aes(xmin=averages-ranges,xmax=averages+ranges))

Gives you a plot like:

Dot plot with ranges

2
  • 1
    fantastic! and just to make sure I understand, the last line should be (ranges/2) each time, to get half the range on each side of the average, correct?
    – Pascal
    Apr 1, 2012 at 6:35
  • Depends on how you have defined the ranges, but if they represent an actual range (the difference between minima and maxima) than yes. If they are standard error figures, than how I've treated them above is what you need. Apr 1, 2012 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.