Bars not anchored at zero using gap.barplot

490 views Asked by At

I am using the gap.barplot() function from plotrix, and I've been having a problem when some values are very small. Starting with the following dataframe:

test.df <- data.frame(
perc = c(2.5,7.5,12.5,17.5,22.5,27.5,32.5,37.5,42.5,47.5,52.5,57.5,62.5,67.5,72.5,77.5,82.5,87.5,92.5,97.5), 
count = c(143649,30872,17444,11333,7513,5257,3677,2795,1976,1530,1022,916,862,985,808,339,59,2,1,1)
)

If I create a gapped bar plot as follows:

library(plotrix)
gap.barplot(test.df$count, gap=c(38000,136000), xaxlab=test.df$perc, xtics=test.df$perc, ytics=c(0:30)*5000, col="white", las=1)

it comes out looking fine.

enter image description here

However, if I try to divide the "count" values by 1000, using the following code:

gap.barplot(test.df$count/1000, gap=c(38,136), xaxlab=test.df$perc, xtics=test.df$perc, ytics=c(0:30)*5, col="white", las=1)

the bars are not anchored at 0.

enter image description here

All "count" values greater than 3000 (or 3 on the new scale) are downward pointing even though they are greater than zero. For some reason the bars are being anchored at 3 rather than 0. Does anyone know why this would happen and what I can do to fix it?

1

There are 1 answers

0
danielson On BEST ANSWER

I just looked into this. It looks like the issue has been fixed in the newer version of the package. When I installed plotrix using the following command from the devtools pacakge, I was able to produce the correct plot.

install_github("cran/plotrix")

enter image description here

It appears as though the bottom horizontal cut was set with the following code prior to the code update:

botgap <- ifelse(gap[1] < 0, gap[1], xlim[1])

and was recently updated to the following:

botgap<-ylim[1]

In your example, I was seeing the package calculate botgap=2.5 (so all values below 2.5 appeared with the negative bars) but you wanted it at 0. I don't see a way to override the value of botgap in the prior version of the package, so you might want to grab the newer version, if possible. Another option would be to create a new function defined by you with the correct code.

If you want to check for yourself, you can use plotrix::gap.barplot in R console to see the function you downloaded and compare the code to the newer version on github here.