I have a data frame:
m.All_Tissues <- structure(list(Sample = c("1: FL_643", "2: FL_645", "3: FL_647", "4: FL_656", "5: FL_658", "6: cKO_644", "7: cKO_646", "8: cKO_654", "9: cKO_655", "10: cKO_657", "1: FL_643", "2: FL_645", "3: FL_647", "4: FL_656", "5: FL_658", "6: cKO_644", "7: cKO_646", "8: cKO_654", "9: cKO_655", "10: cKO_657", "1: FL_643", "2: FL_645", "3: FL_647", "4: FL_656", "5: FL_658", "6: cKO_644", "7: cKO_646", "8: cKO_654", "9: cKO_655", "10: cKO_657"), Genotype = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("miR-15/16 FL", "miR-15/16 cKO"), class = "factor"), Tissue = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("iLN", "Spleen", "Skin"), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Cells/SC/Live/CD8—,, CD4+,Freq. of Parent", class = "factor"),
value = c(41.2, 35.5, 39.5, 33.2, 39.1, 35.5, 35.7, 33.9,
39.7, 42.4, 23.3, 18.4, 20.9, 15.5, 19, 20.5, 22.5, 21.8,
23.8, 24.6, 28.8, 16.9, 21.4, 19.5, 25.4, 27.1, 31.3, 28.8,
52.8, 19)), .Names = c("Sample", "Genotype", "Tissue", "variable", "value"), row.names = 101:130, class = "data.frame")
and I would like to use a function call where I can specify objects as arguments before the objects are created to make the function more flexible.
What I have working is this:
library(dplyr)
library(ggplot2)
plot_it <- function(subsets = NULL,
row_add = NULL) {
temp <- droplevels(m.All_Tissues[m.All_Tissues$Tissue %in% subsets,])
rownames(temp) <- NULL
df <- droplevels(temp[c(row_add),])
rownames(df) <- NULL
color.groups <- c("black","red")
names(color.groups) <- unique(df$Genotype)
shape.groups <- c(16, 1)
names(shape.groups) <- unique(df$Genotype)
dmax = df %>% group_by("Tissue") %>%
summarise(value = max(value, na.rm = TRUE),
Genotype = NA)
ggplot(df, aes(x = Tissue, y = value, color = Genotype, shape = Genotype)) +
geom_boxplot(position = position_dodge(width = 0.75)) +
scale_color_manual(values = color.groups) +
scale_shape_manual(values = shape.groups)
}
plot_it(subsets = c("Spleen", "iLN"), row_add = c(1:20))
But I would like something that looks more like this:
plot_it <- function(data.set = NULL,
subsets = NULL,
group.by = NULL,
comparison = NULL,
row_add = NULL) {
temp <- droplevels(data.set[data.set$group.by %in% subsets,])
rownames(temp) <- NULL
df <- droplevels(temp[c(row_add),])
color.groups <- c("black","red")
names(color.groups) <- unique(df$comparison)
shape.groups <- c(16, 1)
names(shape.groups) <- unique(df$comparison)
dmax = df %>% group_by(group.by) %>%
sumarise(value = max(value, na.rm = TRUE),
comparison = NA)
ggplot(df, aes(x = group.by, y = value, color = comparison, shape = comparison)) +
geom_boxplot(position = position_dodge(width = 0.75)) +
scale_color_manual(values = color.groups) +
scale_shape_manual(values = shape.groups)
}
plot_it(data.set = m.All_Tissues, subsets = c("Spleen", "iLN"), group.by = "Tissue", comparison = "Genotype", row_add = c(1:20))
I'm not sure how to go about this and would really appreciate any help or pointers in the right direction!