[R] barchart error (invalid line type) maybe caused by mangled data frame, & fix() resolving the problem

Thomas sanobast-r01 at yahoo.de
Wed Mar 2 10:44:04 CET 2005


Hi,

I use R 2.0.1 at WinXP. I ran into the following problem when using 
barchart() and would like to ask if there is a solution for it other 
than using fix().

My data is:

>> class(b)
> [1] "data.frame"
>> unclass(b)
> $uint
>  [1]  84  42  71  82  80 123  13   1  15  77  61  56  62  27 100  87 110 191  71
> [20]   3  48 144 101 115 161  52 164 105 154 266  93   1  53 167 161 130 195
> 
> $uext
>  [1]  17  12  13  17  14  32  34   5  50 132  93  66  86  66  65  63  54  97  34
> [20]  41  81 123  88  85 213 159 171 158 140 153  75  65 126 252 225 127 192
> 
> $uges
>  [1] 101  54  84  99  94 155  47   6  65 209 154 122 148  93 165 150 164 288 105
> [20]  44 129 267 189 200 374 211 335 263 294 419 168  66 179 419 386 257 387
> 
> $month
>  [1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "01" "02" "03"
> [16] "04" "05" "06" "07" "08" "09" "10" "11" "12" "01" "02" "03" "04" "05" "06"
> [31] "07" "08" "09" "10" "11" "12" "01"
> 
> $year
>  [1] "02" "02" "02" "02" "02" "02" "02" "02" "02" "02" "02" "02" "03" "03" "03"
> [16] "03" "03" "03" "03" "03" "03" "03" "03" "03" "04" "04" "04" "04" "04" "04"
> [31] "04" "04" "04" "04" "04" "04" "05"
> 
> attr(,"row.names")
>  [1] "1"   "11"  "12"  "13"  "14"  "15"  "16"  "17"  "18"  "19"  "110" "111"
> [13] "112" "113" "114" "115" "116" "117" "118" "119" "120" "121" "122" "123"
> [25] "124" "125" "126" "127" "128" "129" "130" "131" "132" "133" "134" "135"
> [37] "136"

or simply:

 > b
     uint uext uges month year
1     84   17  101    01   02
11    42   12   54    02   02
12    71   13   84    03   02
13    82   17   99    04   02
14    80   14   94    05   02
15   123   32  155    06   02
16    13   34   47    07   02
17     1    5    6    08   02
18    15   50   65    09   02
19    77  132  209    10   02
110   61   93  154    11   02
111   56   66  122    12   02
112   62   86  148    01   03
113   27   66   93    02   03
114  100   65  165    03   03
115   87   63  150    04   03
116  110   54  164    05   03
117  191   97  288    06   03
118   71   34  105    07   03
119    3   41   44    08   03
120   48   81  129    09   03
121  144  123  267    10   03
122  101   88  189    11   03
123  115   85  200    12   03
124  161  213  374    01   04
125   52  159  211    02   04
126  164  171  335    03   04
127  105  158  263    04   04
128  154  140  294    05   04
129  266  153  419    06   04
130   93   75  168    07   04
131    1   65   66    08   04
132   53  126  179    09   04
133  167  252  419    10   04
134  161  225  386    11   04
135  130  127  257    12   04
136  195  192  387    01   05

What I would like to do is this: display a bar chart uint per month with 
the values per year stacked. So I type:

> barchart(uint ~ month, data=b, stack=TRUE, horizontal=F, groups=year,
>         xlab="Month", ylab="Count"
> )

The trellis window pops up and I after drawing one bar I get this error 
message:

> Error in grid.Call.graphics("L_rect", x$x, x$y, x$width, x$height, valid.just(x$just)) : 
>         invalid line type

I tried several things to resolve the problem. Rather by accident I typed:

> fix(b)

After trying the barchart() function again, it was properly displayed.

I guess that the data.frame is somehow messed up during the aggregation 
procedure, which didn't cause any problems so far, and that fix() 
somehow puts things right again. Now, is there a non-interactive 
solution for this problem? Does anybody know what caused this problem in 
the first place?

The data is read from a bundle of csv files using this function:

> cls.read.data <- function (name, months=months.default, only.all=F, ...) {
>     datavars <- paste(name, months, sep="")
>     
>     for (m in months) {
>         v <- paste(name, m, sep="")
>         filename <- paste(data.dir, paste(v, ".txt", sep=""), sep="/")
>         print(filename)
>         data <- tryCatch(read.table(filename, ...), error=function(filename, ...) {data.frame()})
>         if (nrow(data) > 0) {
>             data[['month']] <- m
>         }
>         assign(v, data)
>     }
>    
>     fn <- function(m) {eval(parse(text=m))}
>     rv <- lapply(datavars, fn)
>     rv$all <- do.call("rbind", rv)
>     if (only.all) {
>         rv$all
>     } else {
>         rv
>     }
> }
> 
> ben.cols <- c("uint", "uext", "uges")
> months.all <- c(
>     "0201", "0202", "0203", "0204", "0205", "0206", "0207", "0208", "0209", "0210", 
>     "0211", "0212", "0301", "0302", "0303", "0304", "0305", "0306", "0307", "0308", 
>     "0309", "0310", "0311", "0312", "0401", "0402", "0403", "0404", "0405", "0406", "0407",
>     "0408", "0409", "0410", "0411", "0412", "0501",
> )
> bb <- cls.read.data("benutzer", header=F, sep=";", quote="", col.names=ben.cols, month=months.all)
 > b <- bb$all
> b$month <- substring(bb$all$month, 3)
> b$year <- substring(bb$all$month, 1, 2)

I'm (still) new to R, so I guess there are more appropriate ways to 
collect the data.

Regards,
Thomas.




More information about the R-help mailing list