[R] Using barplot() with zoo -- names.arg not permitted?

David Wolfskill r at catwhisker.org
Thu Mar 17 21:54:35 CET 2011


On Thu, Mar 17, 2011 at 10:23:33AM -0400, Gabor Grothendieck wrote:
> On Thu, Mar 17, 2011 at 9:38 AM, David Wolfskill <r at catwhisker.org> wrote:
> ...
> > But the X-axis labels show up as "large integers" -- the POSIXct values
> > are apparently treated as numeric quantities for this purpose.
> ...
> Please cut this all down to as small a reproducible example as you can
> manage and place it all in ***one*** code section that also generates
> the data so that we can copy a single section of code from your post
> to R.  Use dput to convert data to code for this purpose.

Thank you for that.  :-)

The data below is a bit larger than absolutely necessary, but its
much smaller than what I would normally be using -- and I was able
to paste this into R directly (on a different machine, getting the
same results), so I believe it will suffice.

As a benefit, after going through this, I did manage to find an
invocation that is able to generate an acceptable X-axis -- at
least, sometimes.  (Changing the size of the X11 window appears to
mess things up -- at least.)  But this seems rather more complicated
than I expected.

Anyway, here's a cut/paste of the requested code section:

df <- structure(list(time = c(1297278016L, 1297278017L, 1297278018L, 
1297278019L, 1297278020L, 1297278021L, 1297278022L, 1297278023L, 
1297278024L, 1297278025L, 1297278026L, 1297278027L, 1297278028L, 
1297278029L, 1297278030L, 1297278031L, 1297278032L, 1297278033L, 
1297278034L, 1297278035L, 1297278036L, 1297278037L, 1297278038L, 
1297278039L, 1297278040L, 1297278041L, 1297278042L, 1297278043L, 
1297278044L, 1297278045L, 1297278046L, 1297278047L, 1297278048L, 
1297278049L, 1297278050L, 1297278051L, 1297278054L, 1297278055L, 
1297278056L, 1297278057L, 1297278058L, 1297278059L, 1297278060L, 
1297278061L, 1297278062L, 1297278063L, 1297278064L, 1297278065L, 
1297278066L, 1297278067L, 1297278068L, 1297278069L, 1297278070L, 
1297278071L, 1297278072L, 1297278073L, 1297278074L, 1297278075L, 
1297278076L, 1297278077L, 1297278078L, 1297278079L, 1297278080L, 
1297278081L, 1297278082L, 1297278083L, 1297278084L, 1297278085L, 
1297278086L, 1297278087L, 1297278088L, 1297278089L, 1297278090L, 
1297278091L, 1297278092L, 1297278093L, 1297278094L, 1297278095L, 
1297278096L, 1297278097L, 1297278098L, 1297278099L, 1297278100L, 
1297278101L, 1297278102L), kern.cp_time_idle = c(3193265L, 3193540L, 
3193771L, 3194037L, 3194267L, 3194458L, 3194572L, 3194602L, 3194602L, 
3194602L, 3194602L, 3194602L, 3194603L, 3194607L, 3194617L, 3194672L, 
3194802L, 3194933L, 3195067L, 3195225L, 3195378L, 3195378L, 3195418L, 
3195418L, 3195418L, 3195419L, 3195419L, 3195421L, 3195422L, 3195422L, 
3195422L, 3195422L, 3195422L, 3195450L, 3195450L, 3195664L, 3196432L, 
3196440L, 3196871L, 3196872L, 3196874L, 3196944L, 3196944L, 3197037L, 
3197130L, 3197130L, 3197132L, 3197159L, 3197159L, 3197162L, 3197166L, 
3197236L, 3197367L, 3197367L, 3197497L, 3197639L, 3197782L, 3197922L, 
3197946L, 3197947L, 3197954L, 3197954L, 3197954L, 3198194L, 3198194L, 
3198204L, 3198205L, 3198256L, 3198265L, 3198269L, 3198269L, 3198269L, 
3198269L, 3198269L, 3198270L, 3198270L, 3198270L, 3198270L, 3198270L, 
3198270L, 3198270L, 3198323L, 3198323L, 3198367L, 3198631L), 
    kern.cp_time_intr = c(5142L, 5142L, 5142L, 5143L, 5145L, 
    5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 
    5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 
    5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 5145L, 
    5146L, 5147L, 5147L, 5147L, 5148L, 5148L, 5149L, 5150L, 5152L, 
    5152L, 5152L, 5153L, 5153L, 5155L, 5156L, 5156L, 5156L, 5156L, 
    5156L, 5156L, 5156L, 5156L, 5156L, 5156L, 5156L, 5156L, 5156L, 
    5156L, 5156L, 5156L, 5156L, 5156L, 5157L, 5157L, 5157L, 5157L, 
    5157L, 5157L, 5157L, 5157L, 5157L, 5157L, 5157L, 5157L, 5157L, 
    5157L, 5157L, 5157L, 5157L, 5157L, 5157L, 5157L, 5157L), 
    kern.cp_time_nice = c(36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
    36L, 36L, 36L, 36L, 36L, 36L), kern.cp_time_sys = c(132583L, 
    132591L, 132608L, 132618L, 132646L, 132667L, 132694L, 132737L, 
    132784L, 132825L, 132833L, 132881L, 132933L, 132977L, 133017L, 
    133059L, 133093L, 133121L, 133147L, 133172L, 133210L, 133220L, 
    133259L, 133302L, 133342L, 133388L, 133431L, 133494L, 133547L, 
    133557L, 133592L, 133631L, 133673L, 133713L, 133765L, 133785L, 
    133798L, 133808L, 133816L, 133857L, 133892L, 133951L, 133960L, 
    134004L, 134043L, 134109L, 134170L, 134206L, 134268L, 134329L, 
    134396L, 134443L, 134486L, 134501L, 134558L, 134611L, 134654L, 
    134704L, 134743L, 134789L, 134824L, 134871L, 134881L, 134898L, 
    134929L, 134994L, 135038L, 135071L, 135126L, 135202L, 135277L, 
    135292L, 135372L, 135440L, 135497L, 135565L, 135623L, 135639L, 
    135680L, 135730L, 135777L, 135825L, 135834L, 135884L, 135899L
    ), kern.cp_time_user = c(545975L, 545976L, 546011L, 546017L, 
    546041L, 546113L, 546260L, 546479L, 546728L, 546987L, 547002L, 
    547256L, 547498L, 547740L, 547980L, 548171L, 548293L, 548418L, 
    548542L, 548645L, 548753L, 548762L, 548973L, 549232L, 549510L, 
    549761L, 550018L, 550243L, 550541L, 550555L, 550815L, 551073L, 
    551354L, 551583L, 551827L, 551883L, 551893L, 551893L, 551893L, 
    552149L, 552407L, 552566L, 552575L, 552723L, 552901L, 553119L, 
    553343L, 553568L, 553796L, 554020L, 554237L, 554419L, 554530L, 
    554533L, 554630L, 554725L, 554823L, 554919L, 555148L, 555411L, 
    555673L, 555938L, 555962L, 555997L, 556261L, 556480L, 556725L, 
    556927L, 557157L, 557377L, 557616L, 557641L, 557919L, 558143L, 
    558379L, 558625L, 558877L, 558921L, 559194L, 559476L, 559723L, 
    559940L, 559961L, 560151L, 560156L)), .Names = c("time", 
"kern.cp_time_idle", "kern.cp_time_intr", "kern.cp_time_nice", 
"kern.cp_time_sys", "kern.cp_time_user"), class = "data.frame", row.names = c(NA, 
-85L))
class(df$time) <- "POSIXct"
oid = "kern.cp_time_"
pat = paste("^", oid, sep = "")
col_vec = c("red", "yellow", "green", "blue")
cpu_states <- c("sys", "intr", "user", "nice", "idle")
require(zoo)
df_d <- diff(zoo(df[, grep(pat, names(df))], order.by = df$time))
d_range <- range(index(df_d))
sub <- paste("[", strftime(d_range[1], format = "%c"), "-",
  strftime(d_range[2], format = "%c"), "]")
df_pct <- sweep(df_d, 1, apply(df_d, 1, sum), "/")[, paste(oid, cpu_states[1:4], sep = "")] * 100
title <- "make -j4 -DNOCLEAN buildworld"
barplot(df_pct, border = NA, col = col_vec, space = 0, main = title,
  sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time")
barplot(df_pct, border = NA, col = col_vec, space = 0, main = title,
  sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time",
  names.arg = index(df_d))
traceback()
barplot(df_pct, border = NA, col = col_vec, space = 0, main = title,
  sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time",
  axisnames = FALSE)
axis.POSIXct(1, index(df_d))
barplot(df_pct, border = NA, col = col_vec, space = 0, main = title,
  sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time",
  axisnames = FALSE)
axis(1, at = pretty(index(df_d)) - as.numeric(d_range[1]),
  labels = strftime(as.POSIXct(pretty(index(df_d)),
  origin = "1970-01-01", tz = "UTC"), format = "%X"))



Some comments:
* The first attempt (barplot invoked with default X-axis labels)
  yields X-axis labels:
  1297278017   1297278035   1297278055   1297278073   1297278091

* The second (barplot() invoked with "names.arg = index(df_d)" yielded
  the same default X-axis labels, along with:
Warning messages:
1: In plot.window(xlim, ylim, log = log, ...) :
  "names" is not a graphical parameter
2: In axis(if (horiz) 2 else 1, at = at.l, labels = names.arg, lty = axis.lty,  :
  "names" is not a graphical parameter
3: In title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) :
  "names" is not a graphical parameter
4: In axis(if (horiz) 1 else 2, cex.axis = cex.axis, ...) :
  "names" is not a graphical parameter
> traceback()
No traceback available 

* The third attempt (invoking barplot() with "axisnames = FALSE",
  then invoking "axis.POSIXct(1, index(df_d))") yielded an X-axis label:
  59:56

* The fourth attempt (invoking barplot() with "axisnames = FALSE",
  then invoking "axis()" after studying "axis.POSIXct" for a bit
  (to try to use the parts of it that seemed to be useful for my
  present purposes) yielded X-axis labels:
  11:00:20    11:00:40    11:01:00    11:01:20    11:01:4

  As thos times reflect my timezone, there will likely be differences
  (explainable by that observation) for others.


I hope to learn what I'm misunderstanding.

I suspect that some of this may be "cultural," in a sense: I work
almost exclusively in UNIX (FreeBSD) environments, and am accustomed
to using (what R calls a) "POSIXct" as my preferred form for
date/timestamps for manipulatio0n; the apparent (frequent) requirement
that I specify the "origin" feels bizarre, as it's always 01 Jan 1970
00:00:00 UTC.  Thus, when I think of a date/time stamp, I tend to think
in terms of time_t, and think of other representations as just that:
merely representations of what is "really" a time_t (or POSIXct).  :-}
(Indeed: I wrote the script that generates the timestamp in the data in
question; it does that by invoking "date +%s".)

Thanks!

Peace,
david
-- 
David H. Wolfskill				r at catwhisker.org
Depriving a girl or boy of an opportunity for education is evil.

See http://www.catwhisker.org/~david/publickey.gpg for my public key.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110317/926d2321/attachment.bin>


More information about the R-help mailing list