我正在使用ggplot2进行函数绘制漂亮的小提琴图,还提供平均值,95%自举置信区间和组比较。当我在函数中使用变量的确切名称时,它将起作用。当我使用其他名称时,会出现著名的“替换具有[x]行,数据具有[y]”错误。
我知道此错误通常是由于使用了不存在的对象而引起的。因此,我怀疑这与局部和全局变量/环境的不同级别有关,尤其是在我的函数中似乎不接受名称不同的参数的rcompanion :: dataSummary函数(对于95%CI)方面,但我陷入困境,无法完全解决。
我的自定义功能:
# Nice Violin Plot Function
niceViolin <- function (Data=Data,Group=Group,Response=Response,ManualColour=F,ylabel,compare=F,comp1=NULL,comp2=NULL) {
class(Data$Response) <- "numeric"
library(rcompanion)
dataSummary <- groupwiseMean(Response ~ Group,
data = Data,
conf = 0.95,
digits = 3,
R = 2000,
boot = TRUE,
traditional = FALSE,
normal = FALSE,
basic = FALSE,
percentile = FALSE,
bca = TRUE)
library(ggplot2)
library(ggsignif)
ggplot(Data, aes(x = factor(Group), y = Response, fill = factor(Group))) +
theme_grey(base_size = 24) +
{if (ManualColour == TRUE) scale_fill_manual(values=c("#00BA38", "#619CFF", "#F8766D"))} +
ylab(ylabel) +
geom_violin() +
geom_point(aes(y = dataSummary$Mean), color = "black", size = 4, data = dataSummary) +
geom_errorbar(aes(y = dataSummary$Mean, ymin = dataSummary$Bca.lower, ymax = dataSummary$Bca.upper),
color = "black", size = 0.5, width = 0.2, data = dataSummary) +
theme(legend.position = "none", axis.title.x=element_blank(),
axis.text.x = element_text(colour="black"), axis.text.y = element_text(colour="black")) +
if (compare == TRUE) {geom_signif(comparisons = list(c(comp1, comp2)),
map_signif_level=TRUE, size= 1.3, textsize=8)
}
}
如果我将变量命名为“ Group”和“ Response”,则它可以正常工作。
Response <- sample(x = 1:20, size = 50, replace = TRUE)
Group <- sample(x = 1:2, size = 50, replace = TRUE)
Dataset <- data.frame(Group,Response)
niceViolin(Data = Dataset, Group = Group, Response = Response, ManualColour = F, ylabel = "Dependent Variable", compare = T, comp1 = 1, comp2 = 2)
如果我用我的专栏命名,则无法使用。
# If I name my column names something else, it doesn't work.
names(Dataset) <- c("Condition","Outcome")
niceViolin(Data = Dataset, Group = Condition, Response = Outcome, ManualColour = F, ylabel = "Dependent Variable", compare = T, comp1 = 1, comp2 = 2)
Error in `$<-.data.frame`(`*tmp*`, "Response", value = numeric(0)) :
replacement has 0 rows, data has 50
我希望当我的列名与函数默认名称的名称不同时,它仍然可以工作。知道为什么不是这种情况,以及如何解决这个问题吗?
答案 0 :(得分:1)
您不需要分别传递整个数据帧和向量(请参见上面的注释)。如果要灵活使用变量名,最快的解决方法可能是:
niceViolin <- function (Group, Response, ManualColour=F, ylabel, compare=F, comp1=NULL, comp2=NULL) {
Data <- data.frame(Group, Response)
然后按如下所示调用函数:
niceViolin(Group = Dataset$Condition, Response = Dataset$Outcome, ManualColour = F, ylabel = "Dependent Variable", compare = T, comp1 = 1, comp2 = 2)