Re: Control Significant Figures in Mean Lables

680 views
Skip to first unread message

Brian Diggs

unread,
Dec 6, 2012, 4:20:59 PM12/6/12
to jrr, ggplot2
On 12/6/2012 8:20 AM, jrr wrote:
> How do I control the number of significant figures in labels created by the
> following?
> stat_summary(aes(label=..fun..), fun.y=mean,
> geom="text", size=2)
> Thanks

Either

stat_summary(aes(label=signif(..fun..,4)), fun.y=mean, geom="text", size=2)

or

stat_summary(aes(label=round(..fun..,4)), fun.y=mean, geom="text", size=2)

depending on whether you really mean significant figures or digits after
the decimal point. See the appropriate help page for signif and round if
you are not sure.

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

jrr

unread,
Dec 10, 2012, 8:01:10 AM12/10/12
to ggp...@googlegroups.com, jrr
Thank you Brian. I am having some difficulty with the documentation of ggplot2. If I ?signif I get help for base R function. However, if I ?label I get nothing. Where are the "aes(label=" parameters documented, and the special variable ..fun.. and other special variables.?  The R help documentation of aes is very cursory, as is the documentation for stat_summary, and other ggplot2 aspects. It seems that one must get most documentation from user groups.

Brian Diggs

unread,
Dec 11, 2012, 2:47:25 PM12/11/12
to jrr, ggplot2
On 12/10/2012 5:01 AM, jrr wrote:
> Thank you Brian. I am having some difficulty with the documentation
> of ggplot2. If I ?signif I get help for base R function.

That's the one that I was referring to.

> However, if I ?label I get nothing. Where are the "aes(label="
> parameters documented, and the special variable ..fun.. and other
> special variables.? The R help documentation of aes is very cursory,
> as is the documentation for stat_summary, and other ggplot2 aspects.
> It seems that one must get most documentation from user groups.

Since label is the name of an argument, it is not surprising that it
does not have its own help page. The help for aes is sparse; the
descriptions of the various aesthetics and what they are/do are in
aes_colour_fill_alpha, aes_group_order, aes_linetype_size_shape, and
aes_position. Unfortunately, label is not described in any of these. The
geoms also have documentation (well, examples) about their individual
aesthetics. For geom_text, label is the actual characters/text drawn or
written.

I could not find any documentation about the special variables in the
standard online documentation. The relevant statement from the ggplot2
book, page 58-59, is:

The names of generated variables [by stats] must be surrounded with
.. when used. This prevents confusion in case the original dataset
includes a variable with the same name as a generated variable, and
it makes it clear to any reader of the code that this variable was
generated by a stat. Each statistic lists the variables that it
creates in its documentation.

In stat_summary, the additional columns are documented as fun.data,
fun.ymin, fun.y, and fun.ymax, though in this case, that documentation
is not right. The actual names are either set by the function called
(for fun.data) or are ymin, y, ymax for the other three. (Also, fun.data
takes a vector, not a data.frame, despite the documentation.)

For an example, this is a generic usage of stat_summary:

ggplot(mtcars, aes(x=cyl, y=mpg)) +
stat_summary(fun.ymax = max, fun.y = mean, fun.ymin = min)

But with a different geom, different aesthetics can be set:

ggplot(mtcars, aes(x=cyl, y=mpg)) +
stat_summary(fun.ymax = max, fun.y = mean, fun.ymin = min,
geom="text", aes(label=..y.., colour=..ymax..))

Not that this is a necessarily practical graph, but it shows how things
are used.

I agree that this would be hard to put together from the online
documentation.
Reply all
Reply to author
Forward
0 new messages