Tamilan
Tamilan

Reputation: 981

variable for title in R graph

When I try to give the title name as a function argument for Z and for legend, it is not working.. I also tried by just giving z. Please advise..

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)

Upvotes: 1

Views: 1534

Answers (3)

IRTFM
IRTFM

Reputation: 263332

If you wanted to construct a title outside of the function you could use an as.expression(z). This would let the title be "The average time with ranges: 1 to 8" and the values in the range would be adjusted "on the fly".

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)

Upvotes: 1

blahdiblah
blahdiblah

Reputation: 33991

Quote AveElapsedTime so that it's treated as a string and not a variable:

drawGraph(AET, lab, "AveElapsedTime")

Upvotes: 1

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

Is AveElapsedTime a variable? If yes,this works:

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

If it's just text, this works:

drawGraph(AET, lab, "AveElapsedTime")

Upvotes: 1

Related Questions