Description Usage Arguments Details Value Author(s) See Also Examples
Function to make back to back bar plots as typically desired for survey data.
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)
|
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_colors |
Vector assinging each |
rightside_resposes |
Vector of |
leftside_resposes |
Vector of |
count_caption |
Logical, whether to plot lower right caption with total N |
zerolin |
Logical, whether to plot a vertical line at x=0 |
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.
ggplot object to which other plotting attributes can easily be added (e.g., title)
Sarah C. Nelson (sarahcn@uw.edu)
https://stackoverflow.com/questions/18158461/grouped-bar-plot-in-ggplot
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")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.