When I boxplot some 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 boxplot with an y axis from 300 to 1000. I would like to have “5:00” to “16:40” there instead, simply the seconds converted to minutes and seconds.
Here’s an attempt. It turns the y axis off first and then converts the data to minutes and adds it to the y-axis as tickmarks.
To provide a little explanation:
axTicks“Computes pretty tickmark locations, the same way as R does internally.” (from?axTicks).%%will give you the remainder after division, while%/%will, if givenx %/% ytell you how many timesxgoes intoy.Finally,
sprintfis used for formatting strings and will pad the calculated seconds value to always append leading0‘s if required, i.e. –2becomes02.