Hafiz Muhammad Shafiq
Hafiz Muhammad Shafiq

Reputation: 8680

log scale error in r during barplot

I have to plot a barplot in R. Following is the data

mime_doc$FREQ_PCNT
[1] 97.89  10.19  1.34  3.50  5.01  0.02  0.00  0.04
> mime_doc$BYTE_PCNT
[1] 90.72  19.82  0.70  5.64  0.05  0.04  0.01  0.01

And following are the commands for barplot

mime_freq = data.frame(mime_doc$FREQ_PCNT, mime_doc$BYTE_PCNT)
barplot(t(mime_freq), beside = TRUE, col = c('blue','red') log = 'y')

It gives following error

Error in barplot.default(mime_doc$FREQ_PCNT, beside = TRUE, col = c("blue",  : 
  log scale error: at least one 'height + offset' value <= 0

Where is the problem?

Upvotes: 1

Views: 780

Answers (1)

Miff
Miff

Reputation: 7941

The seventh value of mime_doc$FREQ_PCNT is zero, so doesn't make sense to plot on a log scale. You can deal with this in a couple of ways.

Replace it with NA

mime_doc<- data.frame(FREQ_PCNT=c(97.89, 10.19, 1.34, 3.50, 5.01, 0.02, NA, 0.04), BYTE_PCNT=c(90.72, 19.82, 0.70, 5.64, 0.05, 0.04, 0.01 , 0.01)) mime_freq = data.frame(mime_doc$FREQ_PCNT, mime_doc$BYTE_PCNT) barplot(t(mime_freq), beside = TRUE, col = c('blue','red'), log = 'y')

Now you get no bar for the value of zero, which seems the most sensible way to visualise it, but that may depend on what you're trying to convey with the plot.

Use an offset

mime_doc<- data.frame(FREQ_PCNT=c(97.89, 10.19, 1.34, 3.50, 5.01, 0.02, 0.00, 0.04), BYTE_PCNT=c(90.72, 19.82, 0.70, 5.64, 0.05, 0.04, 0.01 , 0.01)) mime_freq = data.frame(mime_doc$FREQ_PCNT, mime_doc$BYTE_PCNT) barplot(t(mime_freq), beside = TRUE, col = c('blue','red'), log = 'y', offset=0.0001)

The disadvantage of this approach is that the choice of offset is arbitrary and makes a difference to the overall scaling of the plot. The offset should be much less than the minimum non-zero value to avoid distorting the graph.

See ?boxplot for how the offset works

Upvotes: 2

Related Questions