vonjd
vonjd

Reputation: 4380

How to avoid gaps between bars of histogram in R?

I encountered many similar situations like the following one. I am trying to create a simple histogram like so:

coins.2 <- c(-2,0,0,2)
hist(coins.2,freq=F)

The result is this:

enter image description here

My question
What is the easiest way to draw the histogram without gaps between the bars?

Upvotes: 2

Views: 11343

Answers (1)

Richard
Richard

Reputation: 1294

The easiest way I can think of, is to summarise the results using table and then plot that summary:

barplot(table(coins.2))

or if you wanted to stick to the density output, divide the summary by the total number of observations

barplot(table(coins.2)/length(coins.2))

But please remember that there is a reason for histogram showing that empty space. If these were nominal categories, it would not be much of a problem, but if these are actual values it's best to use the histogram solution and show the empty space.

Upvotes: 9

Related Questions