R: boxplot with seconds and seconds along the y axis

When I have boxplotsome data measured in seconds in R, how can I change the scale of the y axis from seconds to minutes and seconds?

For example, when I do something like this:

data <- c(298, 507, 1008, 346)
boxplot(data)

I get a boxplotwith the y axis from 300 to 1000. Instead, I would like to have "5:00" to "16:40", just seconds converted to minutes and seconds.

+5
source share
2 answers

Here is an attempt. First, it turns off the y axis, and then converts the data into minutes and adds them to the y axis as labels.

data <- c(298, 507, 1008, 346)
boxplot(data, yaxt="n")
at <- axTicks(2)
axis(2, at=at, labels=sprintf("%d:%02d", at %/% 60, at %% 60), las=1)

To give a little explanation:

axTicks"Calculates pretty tick sites, just like R inside itself." (from ?axTicks).

%% , %/% , x %/% y, , x y.

, sprintf , 0, , .. - 2 02.

enter image description here

+8

axis.POSIXct:

data <- c(298, 507, 1008, 346)
data2 <- as.POSIXct(data, origin=cut(Sys.time(), "hours"))
boxplot(data2, pars=list(yaxt="n"))
axis.POSIXct(2, at=pretty(data2), format="%M:%S", las=2)

enter image description here

+3

All Articles