[R] bwplot puts the bars in the wrong place

Peter Ehlers ehlers at ucalgary.ca
Sat Apr 17 17:59:19 CEST 2010


On 2010-04-17 9:01, James Rome wrote:
> Ah,
>
> Well, I thought I put up just the data needed. Sorry, was wrong file.
> And yes I am still climbing up the very steep learning curve hill. I
> attach a better data set. If it gets stripped, it is at
> http://dl.dropbox.com/u/537118/gdf.txt
>
> 1. I put the gdf$tt~gdf$OnHour because in the rest of my code, there are
> other (previous and bigger) incarnations of this data frame with the
> same names. Or does the call automatically assume that the variable
> names are from the frame in the data= statement? How am I to know?
> 2. The plot you enclosed is still not correct. The 0 time things are
> plotted at 1. And I believe that there are no data at 3 am. That is why
> I asked the question, and I think this is also the key to the problem.
> 3. I did create factors as Jun suggested, and they are in OnHFact. But
> using OnHFact instead of OnHour also gives the wrong plot.

"Wrong" plot only in the sense that there is no '3' on the x-axis?
That's because in your attached data (different from that posted
earlier at http://dl.dropbox.com/u/537118/gdf.zip) there are no
observations with OnHFact = 3. Hence, by default, this level is
dropped from the plot. Now, if you read the documentation (where
this is quite clearly explained), you will find that you can set
the argument drop.unused.levels to TRUE.

I must say, it seems a bit much to complain about documentation
that you haven't read. R documentation is sometimes terse, but
I have generally found it to be far superior to what passes for
documentation from, say, MS.

  -Peter Ehlers

>
> The only thing that put the correct data on the correct hours was to
> call xyplot instead of bwplot, with panel.bwplot in the panel function.
>
> Sorry for being so dense,  but I really find it much harder to read R
> documentation, than say, Java documentation. And I have 10 R books
> including R Graphics, which did not shed light on this issue either.
>
> Thanks for the help,
> Jim Rome
>
> On 4/17/10 9:52 AM, David Winsemius wrote:
>> After unzipping that file, http://dl.dropbox.com/u/537118/gdf.zip , I
>> got a structure object that took what seemed to be an inordinately
>> long time to eval-parse wehn assigned to gdf,  but it did eventually
>> produce:
>>
>>> str(gdf)
>> 'data.frame':    2656 obs. of  21 variables:
>>   $ OnDate        :Class 'Date'  num [1:2656] 14222 14221 14334 14424
>> 14519 ...
>>   $ FltOrigDt     : chr  "12/9/2008" "12/8/2008" "3/31/2009"
>> "6/29/2009" ...
>>   $ MkdCrrCd      : chr  "DL" "DL" "DL" "DL" ...
>>   $ MkdFltNbr     : int  742 1517 1517 1517 1517 1699 1065 1777 1777
>> 1777 ...
>>   $ DprtTrpnStnCd : chr  "DEN" "JAX" "JAX" "JAX" ...
>>   $ ArrTrpnStnCd  : chr  "ATL" "ATL" "ATL" "ATL" ...
>>   $ ActualOutLocal: POSIXct, format: "2008-12-09 01:04:00" "2008-12-08
>> 05:37:00" "2009-03-31 05:32:00" ...
>>   $ ActualOffLocal: POSIXct, format: "2008-12-09 01:47:00" "2008-12-08
>> 06:11:00" "2009-03-31 05:43:00" ...
>>   $ ActualOnLocal : POSIXct, format: "2008-12-09 06:10:00" "2008-12-08
>> 06:56:00" "2009-03-31 06:33:00" ...
>>   $ ActualInLocal : POSIXct, format: "2008-12-09 06:18:00" "2008-12-08
>> 07:02:00" "2009-03-31 06:39:00" ...
>>   $ ArrivalGate   : Factor w/ 100 levels "A01","A02","A03",..: 2 2 2 2
>> 2 2 2 2 2 2 ...
>>   $ DepartureGate : chr  "C44" "A7" "A7" "A10" ...
>>   $ Flight        : chr  "DAL742" "DAL1517" "DAL1517" "DAL1517" ...
>>   $ OnHour        : chr  "6" "6" "6" "6" ...
>>   $ OnDateTime    : POSIXct, format: "2008-12-09 06:10:00" "2008-12-08
>> 06:56:00" "2009-03-31 06:33:00" ...
>>   $ Runway        : Factor w/ 10 levels "08L","08R","09L",..: 2 4 4 7 4
>> 7 8 8 1 7 ...
>>   $ Delay         : int  15 8 NA NA NA NA NA NA NA NA ...
>>   $ TaxiTime      :Class 'difftime'  atomic [1:2656] 480 360 360 240
>> 360 420 300 300 540 780 ...
>>    .. ..- attr(*, "units")= chr "secs"
>>   $ passurdt      : num  0 0 0 0 1 0 0 0 0 -7 ...
>>   $ OnHFact       : Ord.factor w/ 24 levels "0"<"1"<"2"<"3"<..: 7 7 7 7
>> 7 8 8 13 13 12 ...
>>   $ tt            : num  8 6 6 4 6 7 5 5 9 13 ...
>>
>>
>>
>> On Apr 16, 2010, at 9:37 PM, James Rome wrote:
>>
>>> On 4/16/2010 8:27 PM, Jun Shen wrote:Jim,
>>>
>>> Try this,
>>>
>>> bwplot(tt~as.factor(OnHour),data=gdf,......)
>>>
>>> Jun Shen from Millipore
>>>
>>> I already tried using a factor, and the data set I enclosed had
>>> gdf$OnHFact which was already a factor. It gave the same wrong plot.
>>>
>>> What did work was to call xyplot instead of bwplot, and to use
>>> panel.bwplot in the panel function:
>>>
>>>         hrs = seq(0, 23, 1)
>>>        hrlabs = as.character(seq(0,23,1))
>>>         g = xyplot(gdf$tt~gdf$OnHour |gdf$Runway, data=gdf,
>>
>> This is a puzzling way to invoke xyplot and may have unforeseen
>> dangers. Generally when one uses a data argument to a plot function,
>> one does not also include that dataframe name in the formula terms on
>> either the LHS or the RHS.
>>
>>> ylab="Taxi
>>> time (min)", main=title, xlab="Hour of day",
>>>             xlim=c(-1, 24), scales=list(x = list(rot=90, cex=.6,
>>> alternating=c(3,3,3,3),
>>>             at=hrs, labels=hrlabs
>>>             )),
>>>             panel=function(x, ...) {
>>>                 panel.grid(h = -1, v = 24)
>>>                 panel.bwplot(x, horizontal=FALSE, col="black",...)
>>>
>>>             }
>>>         )
>>>         print(g)
>>
>> I took your initial code for a bwplot call and took out the extraneous
>> dataframe name in the formula terms and put in your axis code (with
>> more sensible formatting) and got what appears to be your desired plot:
>>
>> bwplot( tt~ OnHour |Runway, data=gdf,
>>           scales=list(x = list(rot=90, cex=.6,
>>                                 alternating=c(3,3,3,3),
>>                                 at=hrs, labels=hrlabs
>>                       )        ),
>>           horizontal=FALSE)
>>
>> Plot attached.
>>
>>>
>>>
>>> But I do not understand why this makes a difference. It has something to
>>> do with the fact that there are no data for some of the hours.
>>
>> I have not been able to figure out what you meant by "bars in the
>> wrong place". You could have been more forthcoming about what you saw
>> as the problem for the Readers of the list. My guess is that it has
>> something to do with not following the specified conventions for
>> arguments to the bwplot formula method.
>>
>>
>>> The difference between the calls  is either a bug, or it should be in
>>> the documentation somewhere obvious. I spent a week on this.
>>
>> To report a bug you need a lot more specifics about your system and
>> better efforts at isolation to minimal cases.
>>
>>>
>>> Thanks,
>>> Jim Rome
>>>
>>> Thanks,
>>> Jim Rome
>>>
>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list