plot point range on y axis

35 views
Skip to first unread message

mrwawa

unread,
Feb 10, 2012, 12:02:27 PM2/10/12
to ggplot2
Hi all,

I have a data set with 22 populations with a mean value along with
CIs.

example

Pop1 .5 .48 .52
....
Pop22 .4 .37 .43

I can plot the data on the x-axis using point range, but can't quite
figure out how to plot the pops on the y-axis with the mean values and
CIs on the x-axis.

I would also like to add a vertical line to demarcate a threshold. And
I would like a candybar.

Any ideas about how to do this?

Wade

Dennis Murphy

unread,
Feb 10, 2012, 12:41:49 PM2/10/12
to mrwawa, ggplot2
Hi:

You should provide a reproducible example so that respondents don't
have to generate their own to help you. That said, does the following
work?

library('ggplot2') # will load plyr in 0.8.9, but not 0.9.0
# library('plyr') # uncomment for 0.9.0

# fake data
DF <- data.frame(gp = rep(LETTERS[1:10], each = 20), y = rnorm(200))

# function to produce a CI and return it as a data frame, along with the mean
cifun <- function(d, level = 0.95) {
quant <- (1 + level)/2
n <- length(d$y)
m <- mean(d$y, na.rm = TRUE)
se <- sd(d$y, na.rm = TRUE)/sqrt(length(d$y))
data.frame(mean = m, lcl = m - qt(quant, n - 1),
ucl = m + qt(quant, n - 1))
}

v <- ddply(DF, .(gp), cifun)

# need to produce a data frame of intercepts if you have more than one
xints <- data.frame(x = c(-2, 2))

ggplot(v, aes(y = gp)) +
geom_point(aes(x = mean), size = 3, colour = 'red') +
geom_errorbarh(aes(x = mean, xmin = lcl, xmax = ucl),
colour = 'red', size = 1, width = 0.2) +
geom_vline(data = xints, aes(xintercept = x), colour = 'blue', size = 1)


Notes:
(1) If you want a confidence level different from 0.95, you pass the
level = argument after the name of the function; e.g., ddply(DF,
.(gp), cifun, level = 0.8)
(2) If you only need one threshold, then you can set the intercept in
geom_vline(), e.g.,
geom_vline(xintercept = 2.2, colour = 'blue', size = 1)

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

mrwawa

unread,
Feb 10, 2012, 12:50:16 PM2/10/12
to ggplot2
That is exactly what I was looking for. Thanks a lot.

And I apologize about not providing a reproducible example, that was
sloppy on my part.

Wade
Reply all
Reply to author
Forward
0 new messages