-1

I'm using this code to generate a plot:

require(lattice)

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")
names(logfile2d) <- c("Date","Client","Operations")
logfile2d <- read.table(file="/var/lib/nfs-estadoclientes/nfsclients-2d.log")
logfile=logfile2d
col=allcol[0:length(levels(logfile$Client))]

svg(filename="/var/lib/nfs-estadoclientes/nfsclients-2d.svg",width=14,height=7)

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

But the generated plot prints way much X scale values, and they can't be read. The X scale values are a date and an hour, on this format: "mounth/day hour:minute". It seems that R don't get these values as numbers, and tries to write down all of them.

> dput(head(logfile$Date))
structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("01/09-06:43", 
"01/09-06:53", "01/09-07:03", "01/09-07:13", "01/09-07:23", "01/09-07:33", 
"01/09-07:43", "01/09-07:53", "01/09-08:03", "01/09-08:13", "01/09-08:23", 
"01/09-08:33", "01/09-08:43", "01/09-08:53", "01/09-09:03", "01/09-09:13", 
"01/09-09:23", "01/09-09:33", "01/09-09:43", "01/09-09:53", "01/09-10:03", 
"01/09-10:13", "01/09-10:23", "01/09-10:33", "01/09-10:43", "01/09-10:53", 
"01/09-11:03", "01/09-11:13", "01/09-11:23", "01/09-11:33", "01/09-11:43", 
"01/09-11:53", "01/09-12:03", "01/09-12:13", "01/09-12:23", "01/09-12:33", 
"01/09-12:43", "01/09-12:53", "01/09-13:03", "01/09-13:13", "01/09-13:23", 
"01/09-13:33", "01/09-13:43", "01/09-13:53", "01/09-14:03", "01/09-14:13", 
"01/09-14:24", "01/09-14:33", "01/09-14:43"), class = "factor")

Maybe the best strategy is to omit intermediate values, but I don't know how. I don't know how to make R aware that these are numeric values neither.

Screenshot of the generated plot

Jorge Suárez de Lis
  • 565
  • 1
  • 10
  • 29
  • You need to play with the argument `scales` in the call to `xyplot`. Experiment a bit until you get the density of tickmarks and labels you want. I think you can modify the text by including a `name = sprintf({your_format_here},data}` entry as well. – Carl Witthoft Jan 09 '13 at 12:25
  • 4
    I think you should make `Date` into a true date variable using the `as.Date` function. See the help, or include `dput(head(logfile$Date))` to get people here to chime in. – Aaron left Stack Overflow Jan 09 '13 at 12:54
  • Ok, tried this but it doesn't work `Operations~Date <- lapply(Operations~Date, as.Date, format="%m/%d-%H:%M")`. I'm posting the output from `dput(head(logfile$Date))` on the main post. – Jorge Suárez de Lis Jan 09 '13 at 13:53
  • I hope you meant `Operations$Date` ? – Carl Witthoft Jan 09 '13 at 13:59
  • I can't get anything to print with `dput(Operations~Date)` or `dput(Operations$Date)`. Anyways, the dates are being displayed with Aaron suggestion, and I think that's the thing. – Jorge Suárez de Lis Jan 09 '13 at 14:06

1 Answers1

1

You actually need strptime and as.POSIXct as you have both date and time information. This should convert your Date column into an actual date/time variable that R will plot correctly. Try your plotting command again after running these.

times <- as.POSIXct(strptime(levels(logfile$Date), format="%m/%d-%H:%M"))
logfile$Date <- times[logfile$Date]
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • Thank you, that solved the problem. Now the dates are displayed as dates. However, it's still not perfect because when I plot data for 2 consecutive days, I get only 4 scale values saying "mon", "mon", "tue", "tue". When I plot data for 2 consecutive weeks, I get only 2 scale values saying "2012", "2013". It's not very nice. Is there any way to make this look better? – Jorge Suárez de Lis Jan 09 '13 at 17:18
  • Perhaps. I suggest making a small reproducible example of that behavior and posting it as a new question. – Aaron left Stack Overflow Jan 09 '13 at 17:19