Learn R Programming

ggallin (version 0.1.2)

geom_cloud: geom_cloud

Description

Draw a normal uncertainty cloud as a ribbon

Draws overlapping ribbons of the same identity to create a cloud of (Gaussian) uncertainty. Similar to an errorbar geom in use, but visually less distracting (sometimes).

Usage

geom_cloud(
  mapping = NULL,
  data = NULL,
  ...,
  na.rm = TRUE,
  steps = 7,
  se_mult = 1,
  max_alpha = 1,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame, and will be used as the layer data. A function can be created from a formula (e.g. ~ head(.x, 10)).

...

Other arguments passed on to layer()'s params argument. These arguments broadly fall into one of 4 categories below. Notably, further arguments to the position argument, or aesthetics that are required can not be passed through .... Unknown arguments that are not part of the 4 categories below are ignored.

  • Static aesthetics that are not mapped to a scale, but are at a fixed value and apply to the layer as a whole. For example, colour = "red" or linewidth = 3. The geom's documentation has an Aesthetics section that lists the available options. The 'required' aesthetics cannot be passed on to the params. Please note that while passing unmapped aesthetics as vectors is technically possible, the order and required length is not guaranteed to be parallel to the input data.

  • When constructing a layer using a stat_*() function, the ... argument can be used to pass on parameters to the geom part of the layer. An example of this is stat_density(geom = "area", outline.type = "both"). The geom's documentation lists which parameters it can accept.

  • Inversely, when constructing a layer using a geom_*() function, the ... argument can be used to pass on parameters to the stat part of the layer. An example of this is geom_area(stat = "density", adjust = 0.5). The stat's documentation lists which parameters it can accept.

  • The key_glyph argument of layer() may also be passed on through .... This can be one of the functions described as key glyphs, to change the display of the layer in the legend.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

steps

The integer number of steps, or equivalently, the number of overlapping ribbons. A larger number makes a smoother cloud at the possible expense of rendering time. Values larger than around 20 are typically not necessary.

se_mult

The ‘multiplier’ of standard errors of the given ymin and ymax. If these are at one standard error, then let se_mult take the default value of 1.

max_alpha

The maximum alpha at the maximum density. The cloud will have alpha no greater than this value.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

Aesthetics

geom_cloud understands the following aesthetics (required aesthetics are in bold):

  • x

  • y

  • ymin

  • ymax

  • fill

Only one of ymin and ymax is strictly required.

Author

Steven E. Pav shabbychef@gmail.com

Details

Assumes that ymin and ymax are plotted at a fixed number of standard errors away from y, then computes a Gaussian density with that standard deviation, plotting a cloud (based on geom_ribbon) with alpha proportional to the density. This appears as a vertical ‘cloud’ of uncertainty. In use, this geom should be comparable to geom_errorbar.

A sample output from geom_cloud:

Figure: geomcloud-1.png

See Also

geom_ribbon: The underlying geom

Examples

Run this code
set.seed(2134)
nobs <- 200
mydat <- data.frame(grp=sample(c(0,1),nobs,replace=TRUE),
  colfac=sample(letters[1:2],nobs,replace=TRUE),
  rowfac=sample(letters[10 + (1:3)],nobs,replace=TRUE)) 
mydat$x <- seq(0,1,length.out=nobs) + 0.33 * mydat$grp
mydat$y <- 0.25 * rnorm(nobs) + 2 * mydat$grp
mydat$grp <- factor(mydat$grp)
mydat$se  <- sqrt(mydat$x)

ggplot(mydat,aes(x=x,y=y,ymin=y-se,ymax=y+se,color=grp)) +
facet_grid(rowfac ~ colfac) + 
geom_line() + 
geom_errorbar() + 
labs(title='uncertainty by errorbar')

ggplot(mydat,aes(x=x,y=y,ymin=y-se,ymax=y+se,fill=grp)) +
facet_grid(rowfac ~ colfac) + 
geom_line() + 
geom_cloud(steps=15,max_alpha=0.85) +
labs(title='uncertainty by cloudr')

Run the code above in your browser using DataLab