我有一个数据框:
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")
我想使用函数调用,在创建对象之前我可以将对象指定为参数,以使函数更加灵活。
我的工作是:
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))
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))
我不确定如何解决这个问题,并且非常感谢任何正确方向的帮助或指示!
答案 0 :(得分:2)
我重新命名了一些论点;删除不必要的部分增加了图书馆
但最重要的部分是我使用dataSet[, groupBy]
从数据框调用列,而在ggplot2中我使用color = get(comparison)
(因为你必须指定轴和颜色/形状名称)。
plot_it <- function(dataSet, subsets, groupBy, comparison, rowAdd) {
library(dplyr)
library(ggplot2)
temp <- dataSet[dataSet[, groupBy] %in% subsets, ]
rownames(temp) <- NULL
df <- temp[rowAdd, ]
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_(groupBy) %>%
# summarise(value = max(value, na.rm = TRUE),
# comparison = NA)
ggplot(df, aes(get(groupBy), value,
color = get(comparison), shape = get(comparison))) +
geom_boxplot(position = position_dodge(width = 0.75)) +
labs(x = groupBy,
color = comparison,
shape = comparison) +
scale_color_manual(values = color.groups) +
scale_shape_manual(values = shape.groups)
}
plot_it(m.All_Tissues, c("Spleen", "iLN"), "Tissue", "Genotype", 1:20)
dmax
你的函数中没有任何内容