使用晶格个性化显示在R上的X轴值 [英] Personalize X axis values display on R using lattice
问题描述
我收集了大量有关日期,客户端及其NFS使用情况的数据.我正在使用格子R软件包进行绘图,有关超级用户的建议一个>.另外,Stackoverflow还帮助我将日期字符串转换为实际日期对象.
I have a huge collection of data with date, client and its NFS usage. I'm using lattice R package for plotting, as adviced on superuser. Also, Stackoverflow helped me on converting the date string to an actual date object.
现在,我的代码是这样:
Now, my code is this:
require(lattice)
logfile <- read.table(file="nfsclients-2d.log")
names(logfile) <- c("Date","Client","Operations")
allcol <- c("blue","chocolate4","cornflowerblue","chartreuse4","brown3","darkorange3","darkorchid3","red","deeppink4","lightsalmon3","yellow","mistyrose4","seagreen3","green","violet","palegreen4","grey","slateblue3","tomato2","darkgoldenrod2","chartreuse","orange","black","yellowgreen","slategray3","navy","firebrick1","darkslategray3","bisque3","goldenrod4","antiquewhite2","coral","blue4","cyan4","darkred","orangered","purple4","royalblue4","salmon")
col=allcol[0:length(levels(logfile$Client))]
svg(filename="/tmp/nfsclients-2d.svg",width=14,height=7)
times <- as.POSIXct(strptime(levels(logfile$Date), format="%m/%d-%H:%M"))
logfile$Date <- times[logfile$Date]
xyplot(Operations~Date,group=Client,data=logfile,jitter.x=T,jitter.y=T,
aspect = 0.5, type = "l",
par.settings=list(superpose.line=list(col=col,lwd=3)),
xlab="Time", ylab="Operations", main="NFS Operations (last 2 days, only clients with >40 operations/sec)",
key=list( text=list(levels(logfile$Client)), space='right',
lines=list(col=col),columns=1,lwd=3,cex=0.75))
dev.off()
输出文件是这个文件(删除图例):
And the output file is this (stripped out the legend):
X轴值在这里不是很有用:"tue","tue","wed","wed".似乎只将第一个有意义的值用作标签.另外一些标签(可能是6个或7个)也将更有用.
The X axis values are not very useful here: "tue" "tue" "wed" "wed". It seems that it only takes the first significative value as label. Some more labels (maybe 6 or 7) would be more useful also.
绘制2周时,情况甚至更糟. X轴上仅显示2个值:"2012""2013".甚至没有重复,只有2个值!
When plotting 2 weeks it's even worse. Only 2 values are displayed on the X axis: "2012" "2013". Not even repeated, only 2 values!
推荐答案
您将需要为此轴构造一个适当的间隔.如果确实是前两天,则可能是这样的:
You will need to construct a proper interval for this axis. If this is really the prior two days then perhaps something like:
interval <- as.POSIXct( Sys.Date() - c(1,3) )
然后,您需要为x轴构造一个scales参数:
Then you need to construct a scales argument for the x-axis:
xyplot(Operations~Date,group=Client,data=logfile,jitter.x=T,jitter.y=T,
aspect = 0.5, type = "l",
scales=list(x=list(at= ....... ,
labels=format( ......, "%H:%M") ),
#rest of code
)
您为.....值添加的内容类似于:
What you put in for the ..... value will be something along the lines of:
seq( interval[2], interval[1], by="4 hour")
这是format.POSIXt
调用返回的结果:
> format( seq( interval[2], interval[1], by="4 hour") , "%H:%M")
[1] "16:00" "20:00" "00:00" "04:00" "08:00" "12:00" "16:00" "20:00" "00:00" "04:00" "08:00" "12:00"
[13] "16:00"
这篇关于使用晶格个性化显示在R上的X轴值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!