b2b_barplot: Back to Back Barplot

Description Usage Arguments Details Value Author(s) See Also Examples

Description

Function to make back to back bar plots as typically desired for survey data.

Usage

1
2
3
4
5
6
b2b_barplot(raw = dat, group_var = NULL, response_levels = c(4:3, 1:2),
  response_labels = c("strongly disagree", "disagree", "strongly agree",
  "agree"), response_colors = c(`strongly disagree` = "#ca0020", disagree =
  "#f4a582", agree = "#92c5de", `strongly agree` = "#0571b0"),
  rightside_responses = 1:2, leftside_responses = 3:4,
  count_caption = TRUE, zeroline = TRUE)

Arguments

raw

Input raw data frame.

group_var

Optional variable by which to group, will create facet wrapped plot

response_levels

Values in data frame, e.g. 1:4

response_labels

Labels for responses in same order as response_levels

response_colors

Vector assinging each response_labels value to a color. Default assigns shades of red to left hand side variables, shades of blue to right hand side variables

rightside_resposes

Vector of response_levels to be plotted on right hand side

leftside_resposes

Vector of response_levels to be plotted on left hand side

count_caption

Logical, whether to plot lower right caption with total N

zerolin

Logical, whether to plot a vertical line at x=0

Details

Input data frame should be limited to the variables you want plotted, typically likert-like scales (level of agreement) or yes/no questions. The mapping between response values (e.g., 1,2,3,4) and response labels ("strongly agree","agree", etc.) is given in function arguments. NOte only a series of statements with the same coding can be plotted at once. If all response value are provided to rightside_responses, typical (not back to back) barplot can be created.

Value

ggplot object to which other plotting attributes can easily be added (e.g., title)

Author(s)

Sarah C. Nelson (sarahcn@uw.edu)

See Also

https://stackoverflow.com/questions/18158461/grouped-bar-plot-in-ggplot

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
library(DTCsurvey)
library(ggplot2)
library(reshape2)
library(dplyr)

# with no grouping
dat <- data.frame(matrix(sample(1:4,60, replace=TRUE), nrow=20,ncol=3))
names(dat) <- paste0("statement",1:ncol(dat))
b2b_barplot(raw=dat)

# with grouping
dat$sex <- c(rep("M",times=10), rep("F",times=10))
b2b_barplot(raw=dat, group_var = "sex")

sarahcn/DTCsurvey documentation built on May 29, 2019, 12:07 a.m.