Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the zero labels in Histogram plot in R?

Tags:

r

histogram

Any tips to remove the zero labels in between the histogram bars?

enter image description here

hist(links$Survey_Duration, breaks = seq(0,50,5), main = "Survey Duration",
     labels = TRUE, border = "black",
     xlab = "Survey", ylim = c(0, 15), col = "gray", las = 1, xaxt='n')

axis(side=1, at=seq(0,50,5), labels=seq(0,50,5))

abline(v = mean(links$Survey_Duration), col = "royalblue", lwd = 1.5)

abline(v = median(links$Survey_Duration), col = "red", lwd = 1.5)

legend(x = "topright", c("Mean", "Median"), col = c("royalblue","red"),
       lwd = c(1.5,1.5))
like image 406
MNour Avatar asked Oct 29 '25 05:10

MNour


1 Answers

How about this?

# modify data so there's zero in one of the bins
mtcars$mpg <- ifelse(mtcars$mpg >= 25 & mtcars$mpg <= 30, NA, mtcars$mpg)

# save plot parameters
h <- hist(mtcars$mpg, plot = FALSE)

# produce plot
plot(h, ylim = c(0, 14))

# add labels manually, recoding zeros to nothing
text(h$mids, h$counts + 1, ifelse(h$counts == 0, "", h$counts))

hist_no_zero

like image 92
Daniel Anderson Avatar answered Oct 31 '25 18:10

Daniel Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!