0

我有一个如下所示的数据框。

    > avg_data
            date  region AveElapsedTime
    1   5/1/2012 preprod             23
    2   5/2/2012    prod             76
    3   5/3/2012    Beta             34
    4   5/4/2012    prod             30
    5   5/5/2012    Beta             22
    6   5/6/2012 preprod             32
    7   5/7/2012    Beta             21
    8   5/8/2012    prod             44
    9   5/9/2012 preprod             45
    10 5/10/2012    Beta             23
    11 5/11/2012    prod             50
    12 5/13/2012    Beta             26
    13 5/14/2012 preprod             33
    14 5/15/2012    Beta             75
    15 5/16/2012 preprod             56
    16 5/17/2012    Beta             32
    17 5/18/2012 preprod             67
    18 5/19/2012    prod             40

当我使用 cbind 时,它没有给出正确的行号..

    > cbind(avg_data[avg_data$region == "preprod", "date" ], avg_data[avg_data$region == "preprod", "AveElapsedTime" ])
         [,1] [,2]
    [1,]    3   23
    [2,]   15   32
    [3,]   18   45
    [4,]    5   33
    [5,]    7   56
    [6,]    9   67

这在绘制图表时会出现问题。我将日期作为 x 轴,将 AveElapsedTime 值作为 Y 轴。不是从 2012 年 5 月 1 日开始第一个绘图,而是从 2012 年 5 月 3 日开始,并尝试根据上面给出的行号进行绘图。

如果显示如下,则图形将正确显示..请指教..

    Rownumber AveElapsedTime
         [,1] [,2]
    [1,]    1   23
    [2,]    6   32
    [3,]    9   45
    [4,]   13   33
    [5,]   15   56
    [6,]   17   67

这是我的代码..现在正在运行..需要更改线条的颜色..

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

3 回答 3

5

似乎您想从数据框中进行简单的提取,其中region=="preprod".

这样做的方法是:

avg_data[avg_data$region=="preprod", c("date", "AveElapsedTime")]

        date AveElapsedTime
1   5/1/2012             23
6   5/6/2012             32
9   5/9/2012             45
13 5/14/2012             33
15 5/16/2012             56
17 5/18/2012             67

要绘制数据,您需要将日期字符串转换为实际日期:

x <- avg_data[avg_data$region=="preprod", c("date", "AveElapsedTime")]
x$date <- as.Date(x$date, format="%m/%d/%Y")
x

         date AveElapsedTime
1  2012-05-01             23
6  2012-05-06             32
9  2012-05-09             45
13 2012-05-14             33
15 2012-05-16             56
17 2012-05-18             67

然后我会ggplot像这样绘制它:

library(ggplot2)
x <- avg_data[avg_data$region=="preprod", c("date", "AveElapsedTime")]
ggplot(x, aes(date, AveElapsedTime)) + geom_bar(stat="identity")

在此处输入图像描述

或在基本图形中:

x <- avg_data[avg_data$region=="preprod", c("date", "AveElapsedTime")]
plot(AveElapsedTime ~ date, data=x)

在此处输入图像描述

于 2012-07-12T07:11:54.283 回答
1

这对你有用吗:

library(lubridate)
avg_data$date <- mdy(avg_data$date)
avg <- data.frame(avg_data[avg_data$region == "preprod", "date" ], avg_data[avg_data$region == "preprod", "AveElapsedTime" ])
plot(avg)
于 2012-07-12T07:11:11.100 回答
1

似乎您只要求将行号作为第一列。如果是这样:

cbind(as.numeric(rownames(avg_data[avg_data$region == "preprod", ])), 
      avg_data[avg_data$region == "preprod", "AveElapsedTime" ])
#      [,1] [,2]
# [1,]    1   23
# [2,]    6   32
# [3,]    9   45
# [4,]   13   33
# [5,]   15   56
# [6,]   17   67 

如果您还想重命名流程中的列:

cbind(Rownames = as.numeric(rownames(
  avg_data[avg_data$region == "preprod", ])), 
      AveElapsedTime = avg_data[avg_data$region == "preprod", 
                                "AveElapsedTime" ])
于 2012-07-12T07:19:44.170 回答