2013-01-09 25 views
3

我有一個巨大的數據與日期,客戶端和它的NFS使用收集。我使用格子R包進行繪圖,as adviced on superuser。另外,Stackoverflow幫助我在converting the date string to an actual date object。現在個性化X軸值顯示在R使用格

,我的代碼是這樣的:

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」「結婚」。它似乎只將第一個有意義的值作爲標籤。一些更多的標籤(也許6或7)也會更有用。

當繪製2周時,情況更糟。 X軸上只顯示2個值:「2012」「2013」​​。甚至沒有重複,只有2個值!

The data I'm plotting

+0

這不是一個回答你的問題,而是一個仍然在R內的替代方法可能是ggplot2(您需要先從CRAN安裝它),它可以提供類似於格的功能,但基於一致的「圖形語法」。出於您的目的,您可能更喜歡默認設置。你的基本命令就像ggplot(logfile,aes(x = Date,y = Operations,color = Client))+ geom_line()。您可以添加scale_color_manual()命令以使用您喜歡的顏色。 –

回答

3

您將需要爲此軸構造適當的間隔。如果這是真正的前2天,然後也許是這樣的:

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

然後,你需要構建爲x軸的尺度參數:

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" 
+0

謝謝,但這似乎並不奏效。 'at'參數應該接收日期列表,而不是字符串列表。傳遞字符串列表實際上會發出錯誤。將這些字符串轉換爲日期再次解決了問題:我現在可以控制_ticks_的數量,但不能控制日期顯示格式。 –

+0

對。我說過我在考慮將'seq(interval [2],interval [1],by =「4 hour」)'作爲'at',然後在格式調用中將相同的東西作爲'labels'。我向你展示瞭如何使用'format'。 –

+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

+0

謝謝!我喜歡這種方式,非常簡單!給人一個合理的陰謀,不會有太多的痛苦。我會在找到時間的時候探索這個。現在,我堅持已經在工作的網格方法。 –