Frage

Some of my data consists of duration values expressed in seconds. I'm using the Duration data type from the lubridate package.

To plot this data I'm calling barchart() from the lattice package. The issue is that the values are displayed back in seconds. Is there any way I can control how the axis labels get formatted?

This figure shows what I'm getting right now:

Current

... which I'm generating using:

barchart(val1 ~ val2, groups=group, mydata, 
         auto.key=list(
           columns=1, 
           space="right", 
           text=c("Pilot 1","Pilot 2")
           ), 
         main="Title",
         xlab="Tasks", ylab="Duration"
)

Instead, I'd like to have something closer to this (in what the y axis labels is concerned):

Intended


Update: Adding some sample csv data:

group,val2,val1
pilot01,t1,429.226015
pilot01,t3,693.795607
pilot02,t1,262.798468
pilot02,t3,325.854107
War es hilfreich?

Lösung

In Lattice the scales arguments control the axis; Probably something along the lines of this untested code:

  ..., scales=list(y=list(at=seq.POSIXt(0, 700, length= 6), 
                          labels=format(seq.POSIXt(0, 700, length= 6), "%H:%M:%S:)
                   ), ...

To get this in tested form I needed to convert the seconds to POSIXt format:

mydata$val1time <- as.POSIXct(mydata$val1, origin="1970-01-01")
barchart(val1time ~ val2, groups=group, mydata, horizontal=FALSE, 
         scales=list( y=list( 
            at=seq.POSIXt(from=as.POSIXct(0, origin="1970-01-01"), 
                          to=as.POSIXct(700, origin="1970-01-01"), length= 12), 
            labels=format( seq.POSIXt(from=as.POSIXct(0, origin="1970-01-01"), 
                    to=as.POSIXct(720, origin="1970-01-01"), length= 12), "%H:%M:%S")
                     )   ), 
       auto.key=list(
           columns=1, 
           space="right", 
           text=c("Pilot 1","Pilot 2")
           ), 
         main="Title", xlab="Tasks", ylab="Duration"
)

enter image description here

And if you needed it to go from 0 upwards you would need to add:

 ..., ylim=range( as.numeric( seq.POSIXt(from=as.POSIXct(0, origin="1970-01-01"), 
                                    to=as.POSIXct(700, origin="1970-01-01"),
                                    length= 12))) ,

For the purposes of showing that an entire set of commands give that result (with the addition of the 'ylim' argument, I am pasting the code I used with the dataframe name dat:

 dat <- read.table(text="group,val2,val1
 pilot01,t1,429.226015
 pilot01,t3,693.795607
 pilot02,t1,262.798468
 pilot02,t3,325.854107", header=TRUE, sep=",")
dat$val1time <- as.POSIXct(dat$val1, origin="1970-01-01")

barchart(val1time ~ val2, groups=group, dat, horizontal=FALSE, 
          scales=list( y=list( at=seq.POSIXt(from=as.POSIXct(0, origin="1970-01-01"), to=as.POSIXct(700, origin="1970-01-01"), length= 12), 
                              labels=format( seq.POSIXt(from=as.POSIXct(0, origin="1970-01-01"), to=as.POSIXct(720, origin="1970-01-01"), length= 12), "%H:%M:%S")
                      )   ), 
        auto.key=list(
            columns=1, 
            space="right", 
            text=c("Pilot 1","Pilot 2")
            ), 
          main="Title", xlab="Tasks", ylab="Duration"
 , ylim=range( as.numeric( seq.POSIXt(from=as.POSIXct(0, origin="1970-01-01"), 
                                    to=as.POSIXct(700, origin="1970-01-01"),
                                    length= 12))))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top