Adding data with extension to points in R

I have a table with middle and interquartile ranges. I would like to create a dotplot where the dot will show this average and the strip will stretch across the dot to show the interquartile range. In other words, the point will be in the middle of the bar, the length of which will be equal to my inter-apartment range data. I work in R.

For instance,

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

where ranges will then be added to this graph as columns.

Any ideas?

Thank!

+5
source share
3 answers

Another method using the 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

+8
source

To write here, we use a lattice solution that 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

+7

ggplot2 :

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))

:

Dot plot with ranges

+4

All Articles