Simple question on geoms

26 views
Skip to first unread message

Pedro Aphalo

unread,
Jun 18, 2010, 11:25:05 AM6/18/10
to ggplot2
Hello!
My question may have a simple answer, as I am just starting to use
ggplot2. I received today the book and I am only finished reading
chapter 2. I have searched also this group.

My question is: is there a geom that would work like boxplot (when
used with a factor plus a continuous variable, but that instead of
boxplots would calculate and plot mean+-se? Of course mean+-sd and
mean and ci would also be useful. It would be nice to have the
possibility to specify the variance as an option (for example when
wanting to use the error variance from an ANOVA from an experiment
with blocks).

If this doesn't exist, how difficult would it be to write such a geom?

For the time being I am using qplot.

Many thanks in advance for any help.

Pedro Aphalo

unread,
Jun 18, 2010, 12:16:23 PM6/18/10
to ggplot2
Hello!

This sort of does what I want, but sets ylim according to the range of
the raw data:

> ppt <- qplot(factor(treatment),height,data=piden.data, colour=I("transparent"))
> ppt + stat_summary(fun.data=mean_cl_normal)

On a one out case limits can be set manually, but I miss automatic
axis limits... just a bit.

Pedro.

Pedro Aphalo

unread,
Jun 19, 2010, 6:25:44 AM6/19/10
to ggplot2
Hello again!

At chapter 6 now....

using qplot with geom="blank" still sets the y-axis limits to the
whole data set even if not plotted. Good in many cases, but not what I
want.

Using ggplot and adding a layer with stat_summary also still sets y-
axis limits to the range of the original data even if not plotted with
any geom.

(Of course, geom="boxplot" also uses the y-limits from raw data, but
this is what is needed anyway.)

So, I think that I was expecting a different behaviour of training,
but most probably the currently implemented one is best. I was just a
bit surprised.

I can always change the limits of the scale, or compute the stats
beforehand and use geom_errorbar.

I am sorry about starting this thread, probably it will not be of much
use to many others!

Dennis Murphy

unread,
Jun 19, 2010, 5:45:26 PM6/19/10
to Pedro Aphalo, ggplot2
Hi:

Here's a simple example of how to produce an error bar plot in ggplot2
with the mean as center and quartiles as endpoints of the error bars.

library(ggplot2)
# Fake some data
d <- data.frame(a = rnorm(50), b = rnorm(50, 2), u = rnorm(50, 4, 3),
                 v = rnorm(50, 5, 5))
# Create a function that returns the first quartile, mean and third quartile
f <- function(x) c(quantile(x, 0.25), mean(x), quantile(x, 0.75))
# Apply it to the columns of d and add the variable names into a data frame
dd <- data.frame(c('a', 'b', 'u', 'v'), t(apply(d, 2, f)))
# rename
names(dd) <- c('x', 'q25', 'm', 'q75')
# Create the scaffolding for a ggplot
g <- ggplot(dd, aes(x = x, y = m, ymin = q25, ymax = q75))
# Add the layers
g + geom_point(size = 3) + geom_errorbar(width = 0.3)
# Horizontal error bars instead
last_plot() + coord_flip()

The size and width can be adjusted to taste and other elements can be added
to the plot. geom_errorbar() requires an x (group), y (center), ymin (lower limit)
and ymax (upper limit). You get to choose the center, upper and lower limits.

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

Pedro Aphalo

unread,
Jun 20, 2010, 10:26:24 AM6/20/10
to ggplot2
Hello once more!

One clarification, just in case someone reads this in the future.
To change the y-axis limits and stat_summary to work as I am
expecting, one would need to use coord_cartesian so that all data is
summarized.
Reply all
Reply to author
Forward
0 new messages