Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr Mutate中出错-通过列循环生成图形_R_Loops_Ggplot2_Dplyr - Fatal编程技术网

dplyr Mutate中出错-通过列循环生成图形

dplyr Mutate中出错-通过列循环生成图形,r,loops,ggplot2,dplyr,R,Loops,Ggplot2,Dplyr,我正在研究RStudio,试图循环通过几行dplyr ggplot脚本来识别异常值,然后绘制一个图并标记异常值。数据的一个子集如下所示: miR_373 miR_30 miR_182 type C1 6.78 -2.88 3.75 control C2 11.88 0.28 7.26 control C3 8.55 -4.08 3.37 control C4 7.60 -2.76 7.6

我正在研究RStudio,试图循环通过几行dplyr ggplot脚本来识别异常值,然后绘制一个图并标记异常值。数据的一个子集如下所示:

    miR_373 miR_30 miR_182        type
C1     6.78  -2.88    3.75     control
C2    11.88   0.28    7.26     control
C3     8.55  -4.08    3.37     control
C4     7.60  -2.76    7.60     control
C5    13.18   2.33   13.18     control
P18   12.62   0.37    7.99     treated
P19    4.60  -7.62   -0.52     treated
P23    9.72   0.07    9.72     treated
P24   10.40  -0.68   10.40     treated
P25   11.08   0.81   11.08     treated
N20    7.35  -4.29    2.48 non_treated
N21    3.06  -2.21    4.59 non_treated
N22    6.05  -4.99    2.15 non_treated
N28   10.44  -0.15   10.44 non_treated
N29   10.59   0.36   10.59 non_treated
以下脚本可以很好地生成一个绘图:

cts <- as.data.frame(read.csv("c:/Users/dat.csv", header = TRUE, row.names = 1))
is_outlier <- function(x) {
  return(x< quantile(x,0.25) - 1.5 * IQR(x) | x>quantile(x,0.75) +1.5 * IQR(x))
}

dat <- cts %>% tibble::rownames_to_column(var="outlier") %>% group_by(type) %>% mutate(is_outlier=ifelse(is_outlier(miR_373), miR_373, as.numeric(NA)))
dat$outlier[which(is.na(dat$is_outlier))] <- as.numeric(NA)

ggplot(dat, aes(y=miR_373, x=factor(type))) + geom_boxplot() + geom_text(aes(label=outlier),na.rm=TRUE,hjust=0.05) + geom_dotplot(binaxis='y', stackdir='center', dotsize=1, binwidth = .5)

我可以告诉你,我在循环中的替代我是不被接受的,但我不知道为什么,也没有找到另一种方法。如有任何建议或进一步阅读,将不胜感激。

使用
tidyverse
软件包编程需要一些时间才能习惯。在本例中,您将
i
视为一个表达式,而实际上它是一个字符串。您可以将其转换为
quosure
,然后在需要时取消报价,如下所示:

for(i in miRs){
    i <- rlang::parse_expr(i)
    dat2 <- cts %>%
        tibble::rownames_to_column(var="outlier") %>%
        group_by(type) %>%
        mutate(is_outlier=ifelse(is_outlier(!!i),
                                 !!i,
                                 as.numeric(NA)))
    dat2$outlier[which(is.na(dat2$is_outlier))] <- as.numeric(NA)
    dev.new()
    p <- ggplot(dat2, aes(y=!!i, x=factor(type))) +
        geom_boxplot() +
        geom_text(aes(label=outlier),
                  na.rm=TRUE,
                  hjust=0.05) +
        geom_dotplot(binaxis='y',
                     stackdir='center',
                     dotsize=1,
                     binwidth = .5)
    print(p)
}
for(和平号中的i){
i%
分组依据(类型)%>%
变异(is_outlier=ifelse)(is_outlier(!!i),
我
as.数字(NA)))

dat2$outlier[is.na(dat2$is_outlier))]我认为使用
purrr
包是在列表、向量或列之间循环的最佳方式。
Error in mutate_impl(.data, dots) : Evaluation error: non-numeric argument to binary operator.
for(i in miRs){
    i <- rlang::parse_expr(i)
    dat2 <- cts %>%
        tibble::rownames_to_column(var="outlier") %>%
        group_by(type) %>%
        mutate(is_outlier=ifelse(is_outlier(!!i),
                                 !!i,
                                 as.numeric(NA)))
    dat2$outlier[which(is.na(dat2$is_outlier))] <- as.numeric(NA)
    dev.new()
    p <- ggplot(dat2, aes(y=!!i, x=factor(type))) +
        geom_boxplot() +
        geom_text(aes(label=outlier),
                  na.rm=TRUE,
                  hjust=0.05) +
        geom_dotplot(binaxis='y',
                     stackdir='center',
                     dotsize=1,
                     binwidth = .5)
    print(p)
}