20

I'm creating a histogram in R which displays the frequency of several events in a vector. Each event is represented by an integer in the range [1, 9]. I'm displaying the label for each count vertically below the chart. Here's the code:

hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE) 

Unfortunately, the labels are too long, so they are cut off by the bottom of the window. How can I make them visible? Am I even using the right chart?

4 Answers 4

11

Look at help(par), in particular fields mar (for the margin) and oma (for outer margin). It may be as simple as

par(mar=c(5,3,1,1))   # extra large bottom margin
hist(vector, axes = FALSE, breaks = chartBreaks)
axis(1, at = tickMarks, labels = eventTypes, las = 2, tick = FALSE) 
3
  • I know that there's an easy explanation for this, but no matter what I do with mar, oma, or omi, nothing changes. I've tried setting it with par() and passing it into hist(); no dice. Commented Nov 8, 2010 at 22:29
  • 7
    try par(mar = c(9,4,4,2) + 0.1) --- the default margin on side 1 is 5 so Dirk's example isn't actually creating anything larger than normal for side 1 --- it is making sides -4 smaller than normal. You can only set mar, oma etc via a call to par(). Commented Nov 8, 2010 at 23:21
  • This! @GavinSimpson mar needs to be set via a call to par(). I was trying forever by changing it inside the plotting function and wouldn't budge
    – Kay
    Commented Jun 12, 2020 at 22:06
10

This doesn't sound like a job for a histogram - the event is not a continuous variable. A barplot or dotplot may be more suitable.

Some dummy data

set.seed(123)
vec <- sample(1:9, 100, replace = TRUE)
vec <- factor(vec, labels = paste("My long event name", 1:9))

A barplot is produced via the barplot() function - we provide it the counts of each event using the table() function for convenience. Here we need to rotate labels using las = 2 and create some extra space of the labels in the margin

## lots of extra space in the margin for side 1
op <- par(mar = c(10,4,4,2) + 0.1)
barplot(table(vec), las = 2)
par(op) ## reset

A dotplot is produced via function dotchart() and has the added convenience of sorting out the plot margins for us

dotchart(table(vec))

The dotplot has the advantage over the barplot of using much less ink to display the same information and focuses on the differences in counts across groups rather than the magnitudes of the counts.

Note how I've set the data up as a factor. This allows us to store the event labels as the labels for the factor - thus automating the labelling of the axes in the plots. It also is a natural way of storing data like I understand you to have.

2
  • If one were using hist() the way to decrease the size of the labels would be with cex.axis. It appears the same plotting parameter is used by barplot, but after testing that answer it appears instead to be cex.names since "numeric" labels seem to be treated differently than "character" in barplot().
    – IRTFM
    Commented Nov 9, 2010 at 2:37
  • The bar labels and the count axis labels are drawn in two separate calls to axis(). cex.axis when set in an outer call to par() will change both of these labels because cex.axis and cex.name take par("cex.axis") as their default. If you supply cex.axis to the barplot() call, you haven't changed par("cex.axis"), and hence the labels do not change size. In hist(), you are drawing two numeric axes, drawn at the same time so are both affected by cex.axis. Commented Nov 9, 2010 at 9:00
0

Perhaps adding \n into your labels so they will wrap onto 2 lines? It's not optimal, but it may work.

0

You might want to look at this post from Cross Validated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.