【问题标题】:multiple line graph with difference in the number of elements is not working.also How to Give different colors for all the lines元素数量不同的多线图不起作用。如何为所有线赋予不同的颜色
【发布时间】:2012-07-05 11:21:11
【问题描述】:

我的数据如下。我需要根据区域绘制图表。如果我们根据区域进行提取,每个列表的元素数量不同。

    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 调用时如何为多余的线条赋予多种颜色?

【问题讨论】:

  • 目前还不完全清楚你想做什么——我在下面的回答是我阅读你想法的最佳尝试。 (但在发布示例数据和代码方面做得很好。)

标签: r


【解决方案1】:

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

【讨论】:

  • 您能否建议我是否可以下载该软件包并将其安装在服务器中,而不是直接从 cran 获取该命令,因为我们的服务器位于防火墙后面。
  • 请告知如何在 plot 而不是 ggplot 中完成?
  • 为什么?我使用ggplot,因为它易于使用且功能强大。在基本图形中做到这一点需要我很长时间。
  • 其实我没有权限在我们的服务器上安装ggp​​lot。我试图安装在我的主目录中。但它给出了错误说惰性数据库损坏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多