ggfan (version 0.1.0)

geom_fan: Fan plot visualising intervals of a distribution

Description

Fan Plots allow the distribution of a variable to be visualised by representing sets of central probability intervals through colour. For every value of x, geom_fan computes quantiles of y and uses these to plot intervals containing increasing proportions of the total density of y. Intervals are mapped to a continuous colour scale, so that changes in colour represent intervals covering an increasing proportion of total density. Quantiles can also be precomputed and mapped to the aesthetic quantile. This function is designed with the need to summarise MCMC posterior distributions in mind, and implements the functionality of the fanplot package in ggplot2. Note that there should be enough observations of y at each x to allow estimation of the specified quantiles.

Usage

geom_fan(mapping = NULL, data = NULL, stat = "interval",
  position = "identity", show.legend = NA, inherit.aes = TRUE,
  intervals = (2:98)/100, ...)

Arguments

mapping

Set of aesthetic mappings created by aes or 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.

stat

Use to override the default use of stat_interval

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

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.

intervals

specify the collection of intervals to be represented in the fan.

...

other arguments passed on to layer. These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3. They may also be parameters to the paired geom/stat.

Aesthetics

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

  • x

  • y

  • alpha

  • group

  • quantile

See Also

stat_summary Summarises y at each value of x

stat_quantile Uses quantile regression to predict quantiles

geom_interval Plot intervals boundaries as lines

Examples

Run this code
# NOT RUN {
# Basic use. The data frame must have multiple y values for each
# x
library(ggplot2)

ggplot(fake_df, aes(x=x,y=y)) +geom_fan()


# use precomputed quantiles - reducing storage requirements.
intervals = 1:19/20
fake_q <- calc_quantiles(fake_df, intervals=intervals)
# intervals in geom_fan must be the same as used to compute quantiles.
ggplot(fake_q, aes(x=x,y=y, quantile=quantile)) +
 geom_fan(intervals=intervals)


# change the colour scale
ggplot(fake_df, aes(x=x,y=y)) + geom_fan() + scale_fill_gradient(low="red", high="pink")


# }

Run the code above in your browser using DataCamp Workspace