panel.piechart: Draw pie and ring charts in lattice plots

View source: R/panel_piechart.R

panel.piechartR Documentation

Draw pie and ring charts in lattice plots

Description

This panel function allows to draw pie charts (or rings) while still being able to use the typical lattice way of subsetting data. The function can be used within 'xyplot()' but only one variable needs to be supplied ('x'). Grouping is supported in the sense that the 'x' variable is aggregated (summed up) over each unique group.

Usage

panel.piechart(
  x,
  groups = NULL,
  subscripts = NULL,
  fun = function(x) sum(x, na.rm = TRUE),
  diameter_inner = 0.1,
  diameter_sector = 0.2,
  clockwise = FALSE,
  start_angle = if (clockwise) 90 else 0,
  draw_labels = TRUE,
  col = NULL,
  border = NULL,
  lty = NULL,
  lwd = NULL,
  cex = NULL,
  ...
)

Arguments

x

(numeric, character) variable to be plotted

groups

grouping variable passed down from 'xyplot' (does not need to be specified)

subscripts

subscripts passed down from 'xyplot' (does not need to be specified)

fun

(function) used to perform optional aggregation over groups (default: sum)

diameter_inner

(numeric) diameter of the inner circle of the pie/ring (default: 0.1)

diameter_sector

(numeric) diameter of the outer circle of the pie/ring (default: 0.2)

clockwise

not implemented yet

start_angle

not implemented yet

draw_labels

(logical) switch drawing of labels on or off (default: TRUE)

col, border, lty, lwd, cex

(character, numeric) graphical parameters

...

other arguments passed to the function

Examples

library(grid)
library(lattice)

data("USMortality")

# A simple example using lattice paneling
xyplot( ~ Rate | Sex, USMortality,
  main = "US mortality rates by sex",
  scales = list(draw = FALSE), cex = 0.7,
  panel = function(x, ...) {
    panel.piechart(x, ...)
  }
)

# A more advanced example using grouping and
# adjusting graphical parameters. The main variable 
# 'x' is now summed up for each value of 'groups'
xyplot( ~ Rate | Sex, USMortality,
  groups = gsub(" ", "\n", Cause), 
  col = heat.colors(10),
  border = grey(0.3), cex = 0.7,
  main = "US mortality rates by sex",
  scales = list(draw = FALSE),
  panel = function(x, ...) {
    panel.piechart(x, diameter_sector = 0.1, ...)
  }
)

m-jahn/lattice-tools documentation built on Dec. 3, 2023, 11:14 p.m.