我使用sapply和繪圖。但我無法給我使用sapply繪製的每一行提供不同的顏色。有沒有一種方法可以讓我增加每次sapply一個變量被調用,使用該變量(變量)改變使用plot_colors [變量]顏色計算在sapply中的迭代
plot_colors <- c("blue","red","forestgreen","black")
sapply(unique(ab$region[ab$region]), FUN=graphplot, REG=ab, tl=z, num=num+1)
graphplot <- function(l, REG, tl, num) {
dl <- REG[REG$region == l, tl]
datel <- REG[REG$region == l, "date"]
dl <- cbind(as.numeric(rownames(REG[REG$region == l, ])), REG[REG$region == l, tl])
lines(dl, type="l", pch=2, col=plot_colors[num])
num <- num + 1
}
下面是完整的代碼。
avg_data <- read.table("qes.tbl", header=T, sep=",")
avg_data
# dl <- avg_data[avg_data$region == "prod", "AveElapsedTime"]
#datel <- avg_data[avg_data$region == "prod", "date"]
#Creating the graph pdf in the below path to give as a link in the mail
FL <- 20120631
file <- paste("graph", FL, "pdf", sep=".")
plot_colors <- c("blue","red","forestgreen","black")
pdf(file, height=4.5, width=9.5, onefile=TRUE)
graphplot <- function(l, REG, tl, num) {
dl <- REG[REG$region == l, tl]
datel <- REG[REG$region == l, "date"]
dl <- cbind(as.numeric(rownames(REG[REG$region == l, ])), REG[REG$region == l, tl])
lines(dl, type="l", pch=2, col=plot_colors[num])
num <- num + 1
}
drawGraph <- function(ab, y, z, s) {
#Creating X axis
x <- ab[ab$region == "Beta", z]
y <- ab[,1]
g_range <- range(0,x[!is.na(x)])
plot(NA, type="l", col="orange", xlim= c(1, length(y)), ylim=g_range,axes=FALSE, ann=FALSE)
num=1
sapply(unique(ab$region[ab$region]), FUN=graphplot, REG=ab, tl=z, num)
box()
axis(1, at=1:length(y), lab=FALSE)
text(1:length(y), par("usr")[3] - 2, srt=45, adj=1.2, labels=y, xpd=T, cex=0.3)
scale <- s
axis(2, las=1, at=scale*0:g_range[2], cex.axis=0.3)
main_title<-as.expression(z)
#Caculationg Mean, Upper limit and lower limit using the below commands
MEANLIMIT <- seq(length=length(y), from=mean(x), by=0)
ULIMIT <- seq(length=length(y), from=mean(x) + 2.66*sum(abs(diff(x)))/length(x), by=0)
LLIMIT <- seq(length=length(y), from=mean(x) - 2.66*sum(abs(diff(x)))/length(x), by=0)
lines(MEANLIMIT, type="l", col="black")
lines(ULIMIT, type="l", pch=2, lty=2, col="grey")
lines(LLIMIT, type="l", pch=2, lty=2, col="black")
title(main=main_title, col.main="red", font.main=4)
title(xlab="Test Execution Date", col.lab=rgb(0,0.5,0))
title(ylab="Millisecond", col.lab=rgb(0,0.5,0))
legend("topright", g_range[2], main_title, cex=0.4, col=c("blue"), lty=1);
}
lab<-as.character(avg_data$date)
AET <- avg_data$AveElapsedTime
MTitle <- "AveElapsedTime"
#Creating graph for Average Elapsed time
drawGraph(avg_data, lab, MTitle, 5)
這裏是qes.tbl ..
date,region,AveElapsedTime
5/1/2012,preprod,23
5/2/2012,prod,76
5/3/2012,Beta,34
5/4/2012,prod,30
5/5/2012,Beta,22
5/6/2012,preprod,32
5/7/2012,Beta,21
5/8/2012,prod,44
5/9/2012,preprod,45
5/10/2012,Beta,23
5/11/2012,prod,50
5/13/2012,Beta,26
5/14/2012,preprod,33
5/15/2012,Beta,75
5/16/2012,preprod,56
5/17/2012,Beta,32
5/18/2012,preprod,67
5/19/2012,prod,40
structure(list(date = structure(c(3, 11, 12, 13, 14, 15, 16, 17, 18, 1, 2, 4, 5, 6, 7, 8, 9, 10), .Label = c("5/10/2012", "5/11/2012", "5/1/2012", "5/13/2012", "5/14/2012", "5/15/2012", "5/16/2012", "5/17/2012", "5/18/2012", "5/19/2012", "5/2/2012", "5/3/2012", "5/4/2012", "5/5/2012", "5/6/2012", "5/7/2012", "5/8/2012", "5/9/2012"), class = "factor"), region = structure(c(2, 3, 1, 3, 1, 2, 1, 3, 2, 1, 3, 1, 2, 1, 2, 1, 2, 3), .Label = c("Beta", "preprod", "prod"), class = "factor"), AveElapsedTime = c(23, 76, 34, 30, 22, 32, 21, 44, 45, 23, 50, 26, 33, 75, 56, 32, 67, 40)), .Names = c("date", "region", "AveElapsedTime"), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18"))
發佈一個可重複的版本,我可能會看看。我明白你不想使用ggplot,但你看過晶格而不是基礎圖形嗎? – Andrie 2012-07-12 10:29:59
不是粘貼數據,而是將'dput(qes.tbl)'的結果粘貼到您的問題中。如果你想讓我解決這個問題,請重新制作它。如果我可以複製並粘貼你的代碼,並且它在我的R會話中有效,我會看一看,否則我就繼續前進。有關如何執行此操作的更多幫助,請參閱http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example。 – Andrie 2012-07-12 10:52:52
好,你正在取得進展。你只需要幾步就可以重新制作。現在在'structure ...'之前添加'avg_data < - ',然後將它放在代碼的頂部。然後開始一個乾淨的R會話,複製並粘貼你的代碼。如果它正確運行(複製任何你想讓我們看到的行爲),那麼我應該可以做到這一點。 – Andrie 2012-07-12 11:25:50