Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding spread data to dotplots in R

Tags:

plot

r

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!

like image 691
Pascal Avatar asked Apr 01 '12 05:04

Pascal


People also ask

How to make a dot plot in R?

Dot plot in R 1 The dotchart function. The dotchart function allows to create a Cleveland’s dot plot in R. ... 2 Dot plot by group in R. If you have a variable that categorizes the data in groups, you can separate the dot chart in that groups, setting them in the ... 3 Order dotchart in R by a variable. ... 4 Dumbbell dot plot in R. ...

How to use spread function in R?

How to Use Spread Function in R (With Examples) The spread () function from the tidyr package can be used to “spread” a key-value pair across multiple columns. This function uses the following basic syntax: spread (data, key value)

What are the different types of dot plots?

Note that there are several types of dot charts, like the classical, the Cleveland’s version and Dumbbell dot plots. In this tutorial we are going to show how to create Cleveland dot plots in R and Dumbbell charts.

Can a dot plot be overlaid on a box plot?

Figure 6.31: Dot plot of multiple groups, binning along the y-axis Dot plots are sometimes overlaid on box plots. In these cases, it may be helpful to make the dots hollow and have the box plots not show outliers, since the outlier points will appear to be part of the dot plot (Figure 6.32 ):


2 Answers

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

like image 77
jbaums Avatar answered Nov 04 '22 17:11

jbaums


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

like image 44
Josh O'Brien Avatar answered Nov 04 '22 17:11

Josh O'Brien