R图中标题的变量

时间:2011-11-11 20:57:10

标签: r

当我尝试将标题名称作为Z和传奇的函数参数时,它不起作用..我也试过给z。请指教..

drawGraph <- function(x, y, z) {
    g_range <- range(0,x)
    plot(x, type="o", col="blue", ylim=g_range,axes=FALSE, ann=FALSE)
    box()
    axis(1, at=1:19, lab=FALSE)
    text(1:19, par("usr")[3] - 2, srt=45, adj=1.2, labels=y, xpd=T, cex=0.3)
    axis(2, las=1, at=500*0:g_range[2])
    main_title<-as.character(z)
    title(main=main_title, col.main="red", font.main=4)
    title(xlab="Build", col.lab=rgb(0,0.5,0))
    title(ylab="MS", col.lab=rgb(0,0.5,0))
    legend("topright", g_range[2], c("z"), cex=0.8, col=c("blue"), pch=21, lty=1);
}
drawGraph(AET, lab, AveElapsedTime)

3 个答案:

答案 0 :(得分:1)

AveElapsedTime是变量吗?如果是,这可行:

AveElapsedTime <- 50
drawGraph(AET, lab, AveElapsedTime)

如果它只是文本,则可行:

drawGraph(AET, lab, "AveElapsedTime")

答案 1 :(得分:1)

引用AveElapsedTime,以便将其视为字符串而不是变量:

drawGraph(AET, lab, "AveElapsedTime")

答案 2 :(得分:1)

如果您想在函数之外构建标题,可以使用as.expression(z)。这将使标题为“平均时间范围:1到8”,范围内的值将“动态”调整。

drawGraph <- function(x, y, z) {
    g_range <- range(0,x)
    plot(x, type="o", col="blue", ylim=g_range,axes=FALSE, ann=FALSE)
    box()
    axis(1, at=1:19, lab=FALSE)
    text(1:19, labels=y,par("usr")[3] - 2, srt=45, adj=1.2, xpd=T, cex=0.3)
    axis(2, las=1, at=500*0:g_range[2])
    main_title<-as.expression(z)
    title(main=main_title, col.main="red", font.main=4)
    title(xlab="Build", col.lab=rgb(0,0.5,0))
    title(ylab="MS", col.lab=rgb(0,0.5,0))
    legend("topright", g_range[2], c("z"), cex=0.8, col=c("blue"), pch=21, lty=1);
}
x <- rpois(19, 4)
AveElapsedTime=paste("The average time with ranges:", 
                       round(range(x)[1], 3), 
                        "to" , round(range(x), 3)[1])
lab=1:19
drawGraph(AET, lab, AveElapsedTime)