个性化X轴值使用晶格显示在R上

时间:2013-01-09 18:19:51

标签: r lattice

我有大量的数据集合,包括日期,客户端及其NFS使用情况。我正在使用格子R包进行绘图,as adviced on superuser。此外,Stackoverflow帮我converting the date string to an actual date object

现在,我的代码是:

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()

输出文件就是这个(去掉了图例):

enter image description here

X轴值在这里不是很有用:“tue”“tue”“wed”“wed”。似乎它只将第一个有意义的值作为标签。一些标签(可能是6或7)也会更有用。

当绘制2周时,情况会更糟。 X轴上仅显示2个值:“2012”“2013”​​。甚至没有重复,只有2个值!

data I'm plotting

2 个答案:

答案 0 :(得分:4)

这不是你的格子问题的直接答案,但实际上我会在scales包中使用ggplot2。您可以根据自己的喜好调整轴。

p <- ggplot(dat = logfile, aes(x= Date,
                          y =Operations, 
                          group = Client,
                          color = Client ))+geom_line()

你只给我们2天的数据,所以我在10小时内打破我的数据来提出想法

library(scales) # to access breaks/formatting functions
p %+% scale_x_datetime(breaks = date_breaks("10 hour"), 
                    minor_breaks = date_breaks("2 hour"))

enter image description here

答案 1 :(得分:3)

您需要为此轴构建适当的间隔。如果这真的是前两天那么可能是:

  interval <- as.POSIXct( Sys.Date() - c(1,3) )

然后你需要为x轴构建一个scale参数:

 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
         )

你输入的.....值将是:

   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"