2012-07-05 78 views
0

Graph in which I got我的數據如下所示。我需要根據地區繪製圖表。如果我們根據區域提取,每個列表不具有相同的元素數目。多行圖與元素數量不同的工作.also如何爲所有行提供不同的顏色

avg_data 

    date region AveElapsedTime 
    5/1/2012  Beta 22 
    5/2/2012  Beta 34 
    5/3/2012  Beta 22 
    5/4/2012  Beta 44 
    5/5/2012  Beta 21 
    5/6/2012  Beta 65 
    5/7/2012  Beta 23 
    5/8/2012  Beta 26 
    5/9/2012  Beta 75 
    5/10/2012  Beta 32 
    5/3/2012  preprod 23 
    5/4/2012  preprod 32 
    5/5/2012  preprod 54 
    5/6/2012  preprod 45 
    5/7/2012  preprod 67 
    5/8/2012  preprod 33 
    5/10/2012  preprod 56 
    5/5/2012  prod 44 
    5/6/2012  prod 50 
    5/7/2012  prod 30 
    5/8/2012  prod 40 
    5/9/2012  prod 76 

要conviniently閱讀,粘貼在表格格式

Date Beta preprod prod 
    5/1/2012  22 
    5/2/2012  34 
    5/3/2012  22  23 
    5/4/2012  44  32 
    5/5/2012  21  54  44 
    5/6/2012  65  45  50 
    5/7/2012  23  67  30 
    5/8/2012  26  33  40 
    5/9/2012  75    76 
    5/10/2012  32  56 

這是我的[R腳本

avg_data <- read.table("qes.tbl", header=T, sep=",") 
    avg_data 
      dl <- avg_data[avg_data$region == "prod", "AveElapsedTime"] 
    dl 
      datel <- avg_data[avg_data$region == "prod", "date"] 
    datel 
    #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"] 
      lines(datel, dl, type="l", pch=2, col=plot_colors[num]) 
    } 
    drawGraph <- function(ab, y, z, s) { 
      #Creating X axis 
      x <- ab[ab$region == "Beta", z] 
      y <- ab[ab$region == "Beta", "date"] 
      a <- x 
      g_range <- range(0,x[!is.na(x)]) 
      x[x==0] <- NA 
      plot(which(!is.na(x)),x[!is.na(x)], type="l", col="orange", ylim=g_range,axes=FALSE, ann=FALSE) 
      num=0 
      sapply(unique(ab$region[ab$region != "Beta"]), FUN=graphplot, REG=ab, tl=z, num=num+1) 
      x <- a 
      box() 
      axis(1, at=1:length(x), lab=FALSE) 
      text(1:length(x), 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(x), from=mean(x), by=0) 
      ULIMIT <- seq(length=length(x), from=mean(x) + 2.66*sum(abs(diff(x)))/length(x), by=0) 
      LLIMIT <- seq(length=length(x), 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) 

My圖表不來正確,當我嘗試使用 「線」,以繪製sapply。沒有sapply也沒有工作。它從下一個x軸點開始繪製。例如:促銷日期從2012年5月5日開始。但在圖表中,對於prod,該行從2012年5月6日開始。同樣也適用於preprod。

另外請告知如何通過sapply調用時爲多餘的線條賦予多種顏色?

+0

這不是完全清楚你想做的事 - 我的回答如下是在讀你的心我的最好的嘗試。 (但在發佈示例數據和代碼方面付出了很大的努力) – Andrie 2012-07-05 11:29:10

回答

2

使用ggplot並保存自己很多麻煩:

library(ggplot2) 
avg_data$date <- as.Date(avg_data$date, format="%m/%d/%Y") 

ggplot(avg_data, aes(date, AveElapsedTime)) + geom_line(aes(col=region)) 

enter image description here

+0

您能否告知我是否可以下載該軟件包並將其安裝到服務器中,而不是從該命令直接執行,因爲我們的服務器位於防火牆之後。 – Tamilan 2012-07-05 14:06:48

+0

請告知如何在劇情而不是ggplot中完成? – Tamilan 2012-07-12 07:13:19

+0

爲什麼?我使用'ggplot',因爲它易於使用且功能強大。要在基礎圖形中做到這一點將需要我很長時間。 – Andrie 2012-07-12 07:17:16

相關問題