3

我收集了大量关于日期、客户端及其 NFS 使用情况的数据。根据 superuser 的建议,我正在使用 lattice R 包进行绘图。此外,Stackoverflow 帮助我将日期字符串转换为实际的日期对象

现在,我的代码是这样的:

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

输出文件是这样的(去掉了图例):

在此处输入图像描述

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

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

我正在绘制的数据。

4

2 回答 2

4

这不是您的 lattice 问题的直接答案,但实际上我会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"))

在此处输入图像描述

于 2013-01-09T19:45:24.350 回答
3

您将需要为此轴构造一个适当的间隔。如果这真的是前两天,那么可能是这样的:

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

然后你需要为 x 轴构造一个 scales 参数:

 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"
于 2013-01-09T19:27:56.667 回答