Fixing a y-axis that is too short in barplot function in R

Over the holidays I invested some time in learning R and RStudio, something I have wanted to do for some time now. The opportunity presented itself when needing to help a family member run chi-square tests, something more involved in Excel than I would have guessed (Excel requires you to manually calculate the expected values table). The experience learning R has been fun but there are occasional head-scratching frustrations. As I go through the learning process I’ll be posting here mainly as a reference for me so I can remember how I solved various problems, but hopefully it will provide help to others who have hit stumbling blocks.

One issues I had a problem with was with the barplot function. Take for example this code that contains 100 observations (we’ll say it’s the number of times a person visits a particular store in a month), produces a table from these data, and then creates a barplot from the table.

At first I thought I had a nice looking bar chart, only to notice that, while the data had a maximum frequency of 13, the y-axis cut short at 12. Also when the x-axis legend numbers become double digits they become too wide, leading R to begin skipping every other one. (A low tech way to fix this is widen the plot window.)

The y-axis problem can be solved by manually defining an axis. I tried to set the upper bound to 13, my largest count of observations, but had no luck. Only after setting the maximum to 14 did I have success.

ylim=c(0,14)

And the x-axis label font can be made smaller using:

cex.names =.8

For a final command:

barplot(visits.table, cex.names =.8, ylim=c(0,14))

Which produces this chart:

Better. It wish that, without any tinkering, R would produce a y-axis that extends to the maximum y-axis length.

My next challenge to figure was to show the null values as well (e.g. 16, 17, 18) rather than only values that have data. That can be fixed with “factor”. Example code, that creates a table and produces the barplot in one shot, rather than producing the table first: