stat_hdr: Side-by-side High Density Region (HDR) plots.

Description Usage Arguments Value References Examples

Description

High density region (HDR) boxplots are a variation of boxplots. HDR plots are based on a density estimate of the marginal distribution. Cutoff values for the probability are defined in the parameter probs to define regions. In a uni-modal situation, the HDR plots with probability probs=0.25 show the boxes of a regular boxplot. The code is based on the hdrcde package by Robert Hyndman (1996), who also introduced the plots.

Usage

1
2
3
4
5
6
geom_hdr(mapping = NULL, data = NULL, stat = "hdr", position = "dodge",
  show.legend = NA, inherit.aes = TRUE, width = 0.9, alpha = 0.25, ...)

stat_hdr(mapping = NULL, data = NULL, geom = "hdr", position = "dodge",
  show.legend = NA, inherit.aes = TRUE, width = 0.9, probs = c(0.9, 0.5,
  0.25), ...)

Arguments

mapping

Set of aesthetic mappings created by aes or aes_. If specified and inherit.aes = TRUE (the default), is combined with the default mapping at the top level of the plot. You only need to supply mapping if there isn't a mapping defined for the plot.

data

A data frame. If specified, overrides the default data frame defined at the top level of the plot.

stat

The statistical transformation to use on the data for this layer, as a string.

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.

...

other arguments passed on to layer. There are three types of arguments you can use here:

  • Aesthetics: to set an aesthetic to a fixed value, like color = "red" or size = 3.

  • Other arguments to the layer, for example you override the default stat associated with the layer.

  • Other arguments passed on to the stat.

geom

Use to override the default connection between geom_boxplot and stat_boxplot.

scale

if "area" (default), all vases have the same area (before trimming the tails). If "count", areas are scaled proportionally to the number of observations. If "width", all vases have the same maximum width.

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

Value

A data frame with additional columns:

xmin, xmax, ymin, ymax

coordinates of the boxes specifying each density region.

prob

the probability at which the density is cut - as given in the function call.

mode

the mode of the density

References

Rob J. Hyndman, Computing and Graphing Highest Density Regions, The American Statistician, Vol. 50, No. 2 (May, 1996), pp. 120-126.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require(ggplot2)
data(diamonds)
gghdr(diamonds, color, price)
ggplot(data=diamonds, aes(x=color, y=price)) + geom_hdr(probs=c(0.9, .5, .25), alpha=0.5)
gghdr(diamonds, cut, price, probs=c(50,25,12.5, 6.25, 1), fill=cut) +
  scale_fill_brewer(palette="Set1") +
  scale_colour_brewer(palette="Set1")

p <- ggplot(mtcars, aes(factor(cyl), mpg))

p + geom_hdr()
p + geom_hdr(aes(fill=factor(..probs..)), probs=c(.5, .25, .125)) + scale_fill_brewer()
gghdr(mtcars, factor(cyl), mpg,  probs=c(50,25,12.5))

qplot(factor(cyl), mpg, data = mtcars, geom = "hdr")

p + geom_hdr() + geom_jitter(height = 0)
p + geom_hdr() + coord_flip()
qplot(factor(cyl), mpg, data = mtcars, geom = "hdr") +
  coord_flip()

# Add aesthetic mappings
# Note that hdrs are automatically dodged when any aesthetic is
# a factor
p + geom_hdr(aes(fill = cyl))
p + geom_hdr(aes(fill = factor(cyl)))
p + geom_hdr(aes(fill = factor(vs)))
p + geom_hdr(aes(fill = factor(am)))

# Set aesthetics to fixed value
p + geom_hdr(fill = "grey80", colour = "#3366FF")
qplot(factor(cyl), mpg, data = mtcars, geom = "hdr",
  colour = I("#3366FF"))

x <- rnorm(2000)
group <- rep(1:20, 100)
y <- factor(rep(c(1,2),1000))
qplot(y,x,facets=~group, fill=factor(y), geom="hdr")
qplot(y,x,facets=~group, fill=factor(..probs..), geom="hdr") + scale_fill_brewer()

heike/ggboxplots documentation built on May 17, 2019, 3:23 p.m.