0

我得到的图表我有如下数据。我需要根据区域绘制图表。如果我们基于区域进行提取,则每个列表的元素数量都不相同。

    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

为了方便阅读,以表格形式粘贴

    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)

当我尝试在 sapply 中使用“线条”绘制图表时,我的图表没有正确显示。没有 sapply 它也不起作用。它是从下一个 x 轴点绘制的。例如:生产日期从 2012 年 5 月 5 日开始。但在图表中,对于 prod,这条线从 2012 年 5 月 6 日开始。对于 preprod 也是如此。

另外请告知当我通过 sapply 调用时如何为额外的线条提供多种颜色?

4

1 回答 1

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))

在此处输入图像描述

于 2012-07-05T11:27:37.000 回答