Plotting ‘time’ values
August 30, 2013 Leave a comment
Data in ‘time’ format seems to need a different type of process to plot properly. This code uses strptime to get a proper graph. I have plotted two sets of data on the same plot. This is not visually appealing but serves the purpose. So here the linux ‘pmap’ ouput, ‘Kbytes’ and ‘RSS’ are both plotted against the same time values.
The y-axis label colours differentiate the graphs from one another.
13:07:05 2691296 1600864 1583804 13:08:45 2691296 1601280 1584220 13:10:25 2691296 1601280 1584220 13:12:05 2691296 1601480 1584420 13:13:45 2691296 1601820 1584760 13:15:25 2691296 1601852 1584792 13:17:05 2691296 1601960 1584900 13:18:45 2691296 1601996 1584936 13:20:25 2691296 1603548 1586488
this.dir <- dirname(parent.frame(2)$ofile) setwd(this.dir) data = read.table("D:\\Log analysis\\pmapdata-node1.1",header=F) colnames(data) <- c("Time","Kbytes","RSS","Dirty Mode") png( "pmapanalysis4705.png", width = 2324, height = 868) par(mar=c(7, 9, 6, 8)) plot(strptime(data$Time,"%H:%M:%S"),data$Kbytes,pch=0,type="b",col = "red", col.axis="red", ylab="", xlab="",las=2,lwd=2.5,cex.axis=1.5) title("",cex.main=3,xlab="Time", line=5.2,ylab="Kbytes", cex.lab=2,1) par(new=T) plot(strptime(data$Time,"%H:%M:%S"),data$RSS,col="blue",cex.axis=1.3,col.axis="blue",pch=6,type="b",xaxt="n",ylab="",xlab="",las=2,lwd=2.5) legend("topleft", lty=c(1,1),lwd=c(3.5,3.5),c("Kbytes","RSS"), fill=c("red","blue")) box() dev.off()
One problem with this graph is that very few time values are plotted on the x-axis. I don’t know the reason for this but when I asked the ‘R’ forum Jim Holtman suggested this code.
this.dir <- dirname(parent.frame(2)$ofile) setwd(this.dir) data = read.table("D:\\Log analysis\\pmapdata-node1.1",header=F) colnames(data) <- c("Time","Kbytes","RSS","Dirty Mode") png( "pmapanalysis4705.png", width = 2324, height = 868) par(mar=c(7, 9, 6, 8)) data$tod <- as.POSIXct(paste('2013-08-28', data$Time)) plot(data$tod,data$Kbytes,type="b",col = "blue", ylab="", xaxt = 'n', xlab="",las=2,lwd=2.5, lty=1,cex.axis=1.5) # now plot you times axis(1, at = data$tod, labels = data$Time, las = 2) box() dev.off()
Update : Suggestion from Jim Lemon to use ‘plotrix’
this.dir <- dirname(parent.frame(2)$ofile) setwd(this.dir) data = read.table("D:\\Log analysis\\pmapdata-node1.1",header=F) colnames(data) <- c("Time","Kbytes","RSS","Dirty Mode") png( "pmapanalysis4705.png", width = 2324, height = 868) par(mar=c(7, 9, 6, 8)) plot(strptime(data$Time,"%H:%M:%S"),data$Kbytes,pch=0, type="b",col="red",col.axis="red", ylab="", xlab="",las=2,lwd=2.5,xaxt="n") library(plotrix) staxlab(at=as.numeric(strptime(data$Time,"%H:%M:%S")), labels=as.character(data$Time),nlines=3,srt=90) box() dev.off()