Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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
R qplot、轴标签和循环_R_Ggplot2 - Fatal编程技术网

R qplot、轴标签和循环

R qplot、轴标签和循环,r,ggplot2,R,Ggplot2,我有以下文本文件:trial2.txt 有超过750个数据帧,超过60行。 我将包含数据的一个子集: status cst Hydroxycotinine A1_2_propanediol Bacillus Smoker II, lacto .023 .234 .234 Smoker I, lacto .042

我有以下文本文件:trial2.txt 有超过750个数据帧,超过60行。 我将包含数据的一个子集:

status           cst      Hydroxycotinine     A1_2_propanediol      Bacillus 
Smoker           II, lacto      .023                .234            .234
Smoker           I, lacto       .042                .324            .234
Smoker           III lact       .234                .234            .234
Smoker           II, Lacto      .234                .987            .987
Non smoker       II, Lacto      .533                .987            .234
Non smoker       I, lacto       .234                .342            .972
Non smoker       III, lacto     .782                .234            .897
下面是我用来生成所需图形的代码,不幸的是,在维护变量名的同时,我无法在数据帧中循环:也就是说,当我专门命名要绘制的变量时,下面是生成图形的代码:

require(ggplot2)
trial2<-read.table("pl01.txt",header=TRUE,na.strings='',sep='\t')
 qplot(smoking_status,hydroxycotinine,data=trial2,geom="boxplot")+xlab("")+facet_grid(.~cst,scales="free",space="free")+theme_bw()
require(ggplot2)
trial2
qplot(…)
是一个易于使用的包装器,用于更完整的
ggplot
函数。我建议您放弃它,进入完整的
ggplot
功能集。以下是一种方法(此处仅复制了一个图):

库(ggplot2)
plot.col
    outputs<-(lapply(trial2[3:5],function(x)qplot(smoking_status,trial2$x,data=trial2,geom="boxplot")+xlab("")+facet_grid(.~cst,scales="free",space="free")+theme_bw()))
    outputs
library(ggplot2)
plot.col <- function(col) {             # col is the column **name**
  ggplot(trial2, aes(x=status))+
    geom_boxplot(aes_string(y=col))+    # note use of aes_atring(...)
    xlab("")+
    facet_grid(.~cst,scales="free",space="free")+
    theme_bw()
}
lapply(names(trial2)[3:5],plot.col)