David Wolfskill
2011-Mar-17 13:38 UTC
[R] Using barplot() with zoo -- names.arg not permitted?
I've used barplot(), including the anmes.arg parameter, on data frames successfully, but I'm even newer to using zoo than I am to R. :-} I am working on a functon that accepts a data frame ("df") as its primary argument, extracts information from it to create a zoo, then generates a plot based on that. The data frame has a column called "time" which is a standard UNIX time -- the number of seconds since 01 Jan 1970 (UTC), so I treat that as a POSIXct. (The types of the other columns in the data frame varies greatly, and the sequence in which they occur is not expected to be constant. Thus, I find it easier to let R make a reasonable guess as to the data type, and override special cases -- such as "time" -- on an individual basis.) The data frame contains certain columns whose values are counters, so I generate a zoo of the diff(): oid = "kern.cp_time_" pat = paste("^", oid, sep = "") class(df$time) <- "POSIXct" df_d <- diff(zoo(df[, grep(pat, names(df))], order.by = df$time)) I then save the start & end timestamps for future reference, and generate another zoo, this one containing only the percentages from df_d: cpu_states <- c("sys", "intr", "user", "nice", "idle") d_range <- range(index(df_d)) df_pct <- sweep(df_d, 1, apply(df_d, 1, sum), "/")[, paste(oid, cpu_states[1:4], sep = "")] * 100 Well, that is, I save the percentages in which I'm interested. So far, so good. df_pct is a zoo (as expected, and the content looks reasonable. I then plot it, e.g.: barplot(df_pct, border = NA, col = colvec, space = 0, main = title, sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time") But the X-axis labels show up as "large integers" -- the POSIXct values are apparently treated as numeric quantities for this purpose. And if I try barplot(df_pct, border = NA, col = colvec, space = 0, main = title, sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time", names.arg = index(df_pct)) I am told: 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 which I find a bit perplexing. If I plot via: barplot(df_pct, border = NA, col = col_vec, space = 0, main = title, sub = sub, ylab = "CPU %", ylim = c(0, 100), x lab = "Time", axisnames = FALSE) that works OK, but then I'm unable to generate a human-readable set of X-axis labels: At best, I'm able to get a single label at the origin. Here's an excerpt from the generated df_d: Browse[2]> df_d[1:10] kern.cp_time_idle kern.cp_time_intr kern.cp_time_nice 1297278017 275 0 0 1297278018 231 0 0 1297278019 266 1 0 1297278020 230 2 0 1297278021 191 0 0 1297278022 114 0 0 1297278023 30 0 0 1297278024 0 0 0 1297278025 0 0 0 1297278026 0 0 0 kern.cp_time_sys kern.cp_time_user 1297278017 8 1 1297278018 17 35 1297278019 10 6 1297278020 28 24 1297278021 21 72 1297278022 27 147 1297278023 43 219 1297278024 47 249 1297278025 41 259 1297278026 8 15 Here's a excerpt from the generated df_pct: Browse[2]> df_pct[1:10] kern.cp_time_sys kern.cp_time_intr kern.cp_time_user 1297278017 2.816901 0.0000000 0.3521127 1297278018 6.007067 0.0000000 12.3674912 1297278019 3.533569 0.3533569 2.1201413 1297278020 9.859155 0.7042254 8.4507042 1297278021 7.394366 0.0000000 25.3521127 1297278022 9.375000 0.0000000 51.0416667 1297278023 14.726027 0.0000000 75.0000000 1297278024 15.878378 0.0000000 84.1216216 1297278025 13.666667 0.0000000 86.3333333 1297278026 34.782609 0.0000000 65.2173913 kern.cp_time_nice 1297278017 0 1297278018 0 1297278019 0 1297278020 0 1297278021 0 1297278022 0 1297278023 0 1297278024 0 1297278025 0 1297278026 0 I'm using: R version 2.12.2 (2011-02-25) in a FreeBSD 8.2-STABLE #125 r219696: Wed Mar 16 04:19:54 PDT 2011 environment. I'm willing to share the function (in its current state) and a sample data set (~90KB), but didn't think it would be appropriate to spam the list with either at this point. Thanks! Peace, david -- David H. Wolfskill david 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/e9e697fd/attachment.bin>
Gabor Grothendieck
2011-Mar-17 14:23 UTC
[R] Using barplot() with zoo -- names.arg not permitted?
On Thu, Mar 17, 2011 at 9:38 AM, David Wolfskill <david at catwhisker.org> wrote:> I've used barplot(), including the anmes.arg parameter, on data frames > successfully, but I'm even newer to using zoo than I am to R. ?:-} > > I am working on a functon that accepts a data frame ("df") as its > primary argument, extracts information from it to create a zoo, then > generates a plot based on that. > > The data frame has a column called "time" which is a standard UNIX time > -- the number of seconds since 01 Jan 1970 (UTC), so I treat that as a > POSIXct. ?(The types of the other columns in the data frame varies > greatly, and the sequence in which they occur is not expected to be > constant. ?Thus, I find it easier to let R make a reasonable guess as to > the data type, and override special cases -- such as "time" -- on an > individual basis.) > > The data frame contains certain columns whose values are counters, so I > generate a zoo of the diff(): > > ?oid = "kern.cp_time_" > ?pat = paste("^", oid, sep = "") > ?class(df$time) <- "POSIXct" > ?df_d <- diff(zoo(df[, grep(pat, names(df))], order.by = df$time)) > > I then save the start & end timestamps for future reference, and > generate another zoo, this one containing only the percentages from > df_d: > > ?cpu_states <- c("sys", "intr", "user", "nice", "idle") > ?d_range <- range(index(df_d)) > ?df_pct <- sweep(df_d, 1, apply(df_d, 1, sum), "/")[, paste(oid, cpu_states[1:4], sep = "")] * 100 > > Well, that is, I save the percentages in which I'm interested. > > So far, so good. ?df_pct is a zoo (as expected, and the content looks > reasonable. > > I then plot it, e.g.: > > barplot(df_pct, border = NA, col = colvec, space = 0, main = title, > ?sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time") > > But the X-axis labels show up as "large integers" -- the POSIXct values > are apparently treated as numeric quantities for this purpose. > > And if I try > > barplot(df_pct, border = NA, col = colvec, space = 0, main = title, > ?sub = sub, ylab = "CPU %", ylim = c(0, 100), xlab = "Time", > ?names.arg = index(df_pct)) > > I am told: > > 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 > > which I find a bit perplexing. > > If I plot via: > > barplot(df_pct, border = NA, col = col_vec, space = 0, main = title, > ?sub = sub, ylab = "CPU %", ylim = c(0, 100), x lab = "Time", > ?axisnames = FALSE) > > that works OK, but then I'm unable to generate a human-readable set > of X-axis labels: At best, I'm able to get a single label at the origin. > > Here's an excerpt from the generated df_d: > Browse[2]> df_d[1:10] > ? ? ? ? ? kern.cp_time_idle kern.cp_time_intr kern.cp_time_nice > 1297278017 ? ? ? ? ? ? ? 275 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278018 ? ? ? ? ? ? ? 231 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278019 ? ? ? ? ? ? ? 266 ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? 0 > 1297278020 ? ? ? ? ? ? ? 230 ? ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? 0 > 1297278021 ? ? ? ? ? ? ? 191 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278022 ? ? ? ? ? ? ? 114 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278023 ? ? ? ? ? ? ? ?30 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278024 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278025 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > 1297278026 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? 0 > ? ? ? ? ? kern.cp_time_sys kern.cp_time_user > 1297278017 ? ? ? ? ? ? ? ?8 ? ? ? ? ? ? ? ? 1 > 1297278018 ? ? ? ? ? ? ? 17 ? ? ? ? ? ? ? ?35 > 1297278019 ? ? ? ? ? ? ? 10 ? ? ? ? ? ? ? ? 6 > 1297278020 ? ? ? ? ? ? ? 28 ? ? ? ? ? ? ? ?24 > 1297278021 ? ? ? ? ? ? ? 21 ? ? ? ? ? ? ? ?72 > 1297278022 ? ? ? ? ? ? ? 27 ? ? ? ? ? ? ? 147 > 1297278023 ? ? ? ? ? ? ? 43 ? ? ? ? ? ? ? 219 > 1297278024 ? ? ? ? ? ? ? 47 ? ? ? ? ? ? ? 249 > 1297278025 ? ? ? ? ? ? ? 41 ? ? ? ? ? ? ? 259 > 1297278026 ? ? ? ? ? ? ? ?8 ? ? ? ? ? ? ? ?15 > > > Here's a excerpt from the generated df_pct: > > Browse[2]> df_pct[1:10] > ? ? ? ? ? kern.cp_time_sys kern.cp_time_intr kern.cp_time_user > 1297278017 ? ? ? ? 2.816901 ? ? ? ? 0.0000000 ? ? ? ? 0.3521127 > 1297278018 ? ? ? ? 6.007067 ? ? ? ? 0.0000000 ? ? ? ?12.3674912 > 1297278019 ? ? ? ? 3.533569 ? ? ? ? 0.3533569 ? ? ? ? 2.1201413 > 1297278020 ? ? ? ? 9.859155 ? ? ? ? 0.7042254 ? ? ? ? 8.4507042 > 1297278021 ? ? ? ? 7.394366 ? ? ? ? 0.0000000 ? ? ? ?25.3521127 > 1297278022 ? ? ? ? 9.375000 ? ? ? ? 0.0000000 ? ? ? ?51.0416667 > 1297278023 ? ? ? ?14.726027 ? ? ? ? 0.0000000 ? ? ? ?75.0000000 > 1297278024 ? ? ? ?15.878378 ? ? ? ? 0.0000000 ? ? ? ?84.1216216 > 1297278025 ? ? ? ?13.666667 ? ? ? ? 0.0000000 ? ? ? ?86.3333333 > 1297278026 ? ? ? ?34.782609 ? ? ? ? 0.0000000 ? ? ? ?65.2173913 > ? ? ? ? ? kern.cp_time_nice > 1297278017 ? ? ? ? ? ? ? ? 0 > 1297278018 ? ? ? ? ? ? ? ? 0 > 1297278019 ? ? ? ? ? ? ? ? 0 > 1297278020 ? ? ? ? ? ? ? ? 0 > 1297278021 ? ? ? ? ? ? ? ? 0 > 1297278022 ? ? ? ? ? ? ? ? 0 > 1297278023 ? ? ? ? ? ? ? ? 0 > 1297278024 ? ? ? ? ? ? ? ? 0 > 1297278025 ? ? ? ? ? ? ? ? 0 > 1297278026 ? ? ? ? ? ? ? ? 0 > > > I'm using: > > R version 2.12.2 (2011-02-25) > > in a FreeBSD 8.2-STABLE #125 r219696: Wed Mar 16 04:19:54 PDT 2011 > environment. > > I'm willing to share the function (in its current state) and a sample > data set (~90KB), but didn't think it would be appropriate to spam the > list with either at this point. >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. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com