我正在尝试使用for循环制作一堆图,但似乎无法使其工作。我的y轴应该是变量。
plot(data$date1,data$abc,xlim=c(xmin,xmax),ylim=c(ymin,ymax),cex=.25,
main=reg,xlab="Date",ylab="Percent")
reg
[1] "abc"
plot(data$date1,data$reg,xlim=c(xmin,xmax),ylim=c(ymin,ymax),cex=.25,
main=reg,xlab="Date",ylab="Percent")
有没有办法使用reg来绘制这些数据而不是abc?添加一些示例数据和更多信息。我试图遍历所有的nps并按日期绘制它们。
df <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "11-MAY-2017", class = "factor"),
log = structure(1:6, .Label = c("707406D:01", "707406D:02",
"707406D:03", "707406D:04", "707406D:05", "707406D:06"), class = "factor"),
count = c(1L, 1L, 1L, 1L, 1L, 1L), np. = c(90.465, 89.444,
91.714, 88.082, 89.784, 90.692), npB = c(0.123, 0, 0.122,
0, 0.246, 0.122), npC = c(0, 0, 0.123, 0, 0.126, 0), npD = c(0.248,
0.502, 0, 0.252, 0, 0.492), npE = c(0L, 0L, 0L, 0L, 0L, 0L
), npF = c(0, 0, 0, 0, 0.252, 0), npH = c(0L, 0L, 0L, 0L,
0L, 0L), npI = c(0.341, 0.568, 0.795, 0.908, 0.114, 0.681
)), .Names = c("date", "log", "count", "np.", "npB", "npC",
"npD", "npE", "npF", "npH", "npI"), class = "data.frame", row.names = c(NA,
-6L))
> df
date log count np. npB npC npD npE npF npH npI
1 11-MAY-2017 707406D:01 1 90.465 0.123 0.000 0.248 0 0.000 0 0.341
2 11-MAY-2017 707406D:02 1 89.444 0.000 0.000 0.502 0 0.000 0 0.568
3 11-MAY-2017 707406D:03 1 91.714 0.122 0.123 0.000 0 0.000 0 0.795
4 11-MAY-2017 707406D:04 1 88.082 0.000 0.000 0.252 0 0.000 0 0.908
5 11-MAY-2017 707406D:05 1 89.784 0.246 0.126 0.000 0 0.252 0 0.114
6 11-MAY-2017 707406D:06 1 90.692 0.122 0.000 0.492 0 0.000 0 0.681
for (reg in an) {
plot(data$date1,data$npB,xlim=c(xmin,xmax),ylim=c(ymin,ymax),cex=.25,
main=reg,xlab="Date",ylab="Percent")
graphics.off()
}
在我上面的示例中,我想使用reg变量而不是实际的列名npB来使用for循环。希望这会有所帮助。
答案 0 :(得分:1)
不确定你之后到底是什么,但也许这是在正确的轨道上:
date<- seq(2007:2016)
abc <- sample(1:100, 10)
def <- sample(50:100, 10)
reg <- sample(40:60, 10)
df <- data.frame(date, abc, def, reg)
for(i in names(df[,2:4])){
plot(df$date, df[,i], xlab = "Date", ylab = "Percent", main = i,
ylim = (c(0,100)))
}
修改强>
根据您的评论进行跟进,使用此示例数据:
df2 <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "11-MAY-2017", class = "factor"),
log = structure(1:6, .Label = c("707406D:01", "707406D:02",
"707406D:03", "707406D:04", "707406D:05", "707406D:06"), class = "factor"),
count = c(1L, 1L, 1L, 1L, 1L, 1L), np. = c(90.465, 89.444,
91.714, 88.082, 89.784, 90.692), npB = c(0.123, 0, 0.122,
0, 0.246, 0.122), npC = c(0, 0, 0.123, 0, 0.126, 0), npD = c(0.248,
0.502, 0, 0.252, 0, 0.492), npE = c(0L, 0L, 0L, 0L, 0L, 0L
), npF = c(0, 0, 0, 0, 0.252, 0), npH = c(0L, 0L, 0L, 0L,
0L, 0L), npI = c(0.341, 0.568, 0.795, 0.908, 0.114, 0.681
)), .Names = c("date", "log", "count", "np.", "npB", "npC",
"npD", "npE", "npF", "npH", "npI"), class = "data.frame", row.names = c(NA,
-6L))
对于示例数据,将列名称从npB
拉到结尾:
an <- names(df[,5:ncol(df)])
> an
[1] "npB" "npC" "npD" "npE" "npF" "npH" "npI"
然后,与我展示的第一个示例相比没有太大变化,只需使用an
而不是colnames()
参数:
for(i in an){
plot(df2$date, df2[,i], xlab = "Date", ylab = "Percent", main = i,
ylim = (c(min(df2[,i]),max(df2[,i]))))
}
给出如下图所示的情节: