R qplot、轴标签和循环
我有以下文本文件:trial2.txt 有超过750个数据帧,超过60行。 我将包含数据的一个子集: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
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)
trial2qplot(…)
是一个易于使用的包装器,用于更完整的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)