data1 <- data.frame( "animal" = c(rep("mouse", 10), rep("chipmunk", 10), rep("squirrel", 10)), "weight" = c(rnorm(10, mean = 10, sd = 1), rnorm(10, mean = 50, sd = 5), rnorm(10, mean = 100, sd = 11)) ) # -------------------------------------------------------------------- # -------- Tool for Summary of Means Across Categories --------------- # -------------------------------------------------------------------- # This tool takes a dataset with two variables and calculates the mean # value of a dependent variable (and 95% CI) for each level of an # independent variable. It requires three arguments: # "data" = a dataset containing all the variables for summary) # "groups" = the name (in quotation marks!) of the column containing the categories # "values" = the name (in quotation marks!) of the column containing the values for summary # # example usage: # data1 <- data.frame( "animal" = c(rep("mouse", 10), rep("chipmunk", 10), rep("squirrel", 10)), "weight" = c(rnorm(10, mean = 10, sd = 1), rnorm(10, mean = 50, sd = 5), rnorm(10, mean = 100, sd = 11)) ) sum1 <- summary_table(data = data1, "animal", "weight") library("dplyr") library("rlang") library("lazyeval") ci <- function(x) {sqrt(sd(x)/length(x)) * 1.96} summary_table <- function(data, groups, values) { data %>% group_by(!!sym(groups)) %>% summarise("Mean" = mean(!!sym(values), na.rm = TRUE), "ConfInt" = ci(!!sym(values))) } ################################################# ################################################# ################################################# # -------------------------------------------------------------------- # ----------------- Basic Boxplot ---------------------------- # # This is best done with all the components in a single "data" file # "levels" are the categories for each bar in the barplot (these date are contained within "data") # "levels" should be subsetted from the main data, e.g., data$levels # "meanvals" are the actual means (i.e., the bar height) to be plotted # "meanvals" should be subsetted from the main data, e.g., data$meanvals # "confints" is the 95% confidence interval for each bar # "confints" should be subsetted from the main data, e.g., data$meanvals # "ylabel" should be in QUOTATIONS! # # example usage: data1 <- data.frame("animal" = c("mouse", "chipmunk", "squirrel"), "weight" = c(12, 54, 109), "CI" = c(1.2, 10.1, 24.9)) basic_boxplot(levels = data1$animal, meanvals = data1$weight, confints = data1$CI, ylabel = "weight (g)") # OR basic_boxplot(levels = sum1$animal, meanvals = sum1$Mean, confints = sum1$ConfInt, ylabel = "weight (g)") ##### basic_boxplot <- function(levels, meanvals, confints, ylabel) { plotTop <- max(meanvals + confints) * 1.20 # Top of the plot is 20% higher than tallest CI barCenters <- barplot(meanvals, names.arg = levels, col="gray", las=1, ylim = c(0, plotTop), ylab = ylabel) segments(barCenters, meanvals - confints, barCenters, meanvals + confints, lwd=1) # adding bars arrows(barCenters, meanvals - confints, barCenters, meanvals + confints, lwd=1, angle=90, code=3) # adding braces axis(side = 1, at = barCenters, labels = FALSE) # adding an x-axis line with tick marks abline(h=0) # filling the small gap in the axis }