prettybars2: prettybars2

View source: R/plot_special.R

prettybars2R Documentation

prettybars2

Description

A barplot.

Usage

prettybars2(
  x,
  lab.y = colnames(x),
  n.y = ncol(x),
  lab.group = rownames(x),
  n.group = nrow(x),
  col.group = adjustcolor(seq.int(n.group), alpha.f = 0.5),
  col.line = "skyblue3",
  col.bg = par("bg"),
  cex.y = 0.7,
  notext = FALSE,
  title = paste("prettybar of", m$x),
  sub = paste0("n = ", sum(x)),
  note = NULL,
  subnote = "subnote: here is a subnote",
  legend = 1L,
  ...
)

Arguments

x

a table of data, typically from table or xtabs

lab.y

y-axis labels

n.y

number of y-groups (calculated from x)

lab.group

group labels

n.group

number of groups (calculated from x)

col.group

color for each group level

col.line

color of origin line

col.bg

background color

cex.y

size of lab.y

notext

logical; if TRUE, suppresses all plot text, labels, and axes for user input

title, sub

main and sub titles

note, subnote

optional note (top right) or subnote (bottom right)

legend

integer (0, 1, 2); 0 for no legend; 1 for a legend with pre-formatting; or 2 for a default legend

...

additional graphical parameters passed to par

See Also

prettybars; bibar; bibar

Examples

set.seed(1)
f <- function(...) sample(1:5, 100, replace = TRUE, prob = c(...))
dat <- data.frame(
  q1 = f(.1, .2, .3, .3, .1),
  q2 = f(.1, .4, .1, .3, .1),
  q3 = f(.1, .2, .3, .3, .1),
  q4 = f(.1, .1, .3, .3, .1),
  q5 = f(.2, .1, .2, .3, .2),
  q6 = f(.1, .3, .3, .2, .1),
  q7 = f(.1, .4, .1, .1, .3)
)
dat <- within(stack(dat), {
  values <- factor(values, levels = 1:5, labels = c('NA','SA','A','D','SD'))
})

tdat <- table(dat)
cols <- c(
  grey(.9),
  adjustcolor(c('cadetblue', 'lightblue', 'lightpink', 'red'), alpha.f = 0.5)
)

## compare:
barplot(tdat, horiz = TRUE, las = 1, col = cols, border = NA)
prettybars2(tdat, lab.y = paste('Question #', 1:7), col.group = cols, las = 1L)


## using plotr::bibar
par(mar = c(3, 8, 4, 2))
at <- bibar(t(tdat), 3:2, 4:5, 1, col = cols, xlim = c(-80, 50), axes = FALSE)

axis(1, pretty(c(-80, 50)), abs(pretty(c(-80, 50))))
abline(v = 0, col = 'lightblue', lwd = 4)
text(-85, at, sprintf('Question # %s', 1:7), xpd = NA, adj = 1)

legend('topleft', inset = c(0, -0.1), xpd = NA, fill = cols[2:3],
       legend = rownames(tdat)[2:3], horiz = TRUE, bty = 'n')
legend('topright', inset = c(0, -0.1), xpd = NA, fill = cols[4:5],
       legend = rownames(tdat)[4:5], horiz = TRUE, bty = 'n')


raredd/plotr documentation built on Nov. 19, 2023, 4:09 a.m.