# Plotting a nice y-axis and cropping figures (R)

I was getting lost when editing the post about US measles cases data and so decided recently to cut that post in two. The one on measles is about .. measles. Here, I discuss some code I use to make the y-axis in R figures look a bit better than it does by default. I also show a function for cropping results.

## Making a nice y-axis for plots

The following is a function that I adapted from another I found somewhere on the web (Stack Overflow, almost surely). There is most likely a prettier way to do this, but this one works.

To make the function easier to use, I also use a modified plot function.

The function plot_hr_yaxis returns the parameters for the modified y-axis, so that they can be further used in the plot. To use this function, you need to call it on your data. I show the use with the code for the first of the two plots in the post US measles cases data. The data frame measles contains the loaded measles data.

Note that here, the call to polygon does not involve the “real” y-axis, just the coordinates obtained from par("usr"). In general, one would have to multiply all $y$ values by y_axis\$factor, as is done in the call to lines at the end of the code block.

This gives the first figure in the post US measles cases data, reproduced here for convenience.

## Cropping pdf and png results

The following function is called with the same file name as used for the plot output just after the call to dev.off(). Of course, this requires to have functioning pdftk and convert program line commands available. (Windows people: it can be done. Not my problem here. Maybe I will explain elsewhere.)