`yaxp` is no longer working like it used to

Viewed 30

When I teach histograms using R, I use the yaxp parameter to adjust the y-axis. Here is an example of the code:

hist(bookspending$'Dollars on books', yaxp = c(0,9,9))

This produces a histogram where the y-axis has tick marks at 0, at 9, and a tick mark at every integer in between. Here is a picture of it:

histogram, yaxp works

I'm using Rstudio version 1.4.1717 and R version 4.1.1. This has worked the past 4 years. This semester the yaxp parameter isn't working for some students. I replicated the problem using the web-based version of R. Using the same code, the yaxp parameter makes no changes to the histogram. Here is the picture:

histogram, yaxp does not work

Why does yaxp no longer work for the online version, but it works for my version? How do you adjust the y-axis without yaxp?

1 Answers

This happens because there was a change in the function graphics:::plot.hist in R version 4.2.0.

Previously, the part of the function that controlled the drawing of the axes was simply:

  if (axes) {
     axis(1, ...)
     axis(2, ...)
   }

But it has been changed to

  if (axes) {
     axis(1, ...)            
     yt <- axTicks(2)            
     if (freq && any(ni <- (yt%%1) != 0))                 
         yt <- yt[!ni]            
     axis(2, at = yt, ...)        
   }

Which means that the y axis breaks are now decided solely by the output of the function call axTicks(2). Your yaxp argument is still passed to axis, but unfortunately, specifying at nullifies yaxp.

This is sort of mentioned in the CRAN R News, where it says:

hist.default() gains new fuzz argument, and the histogram plot method no longer uses fractional axis ticks when displaying counts ("Frequency").

So the reason for the change was to prevent fractional axis ticks, but a side effect of this was preventing users from specifying y axis breaks. The only work-around for now it would seem, would be to add the axes manually as suggested in the last set of comments.

You may wish to file this as a bug report, since it seems to me that it should be possible to specify axis breaks and disallow fractional breaks without much difficulty.

Related