Eva
Eva

Reputation: 13

Not show bin count and bar border if value is zero in histogram with ggplot2

I have written an histogram using the ggplot2 package of R. I would like to know if there is a way to not show the bin count and the bar border if the value is zero.

Here is the code of the graph:

ggplot(data=example, aes(example$V1)) + 
geom_histogram(binwidth=1,
               col="black") + 
stat_bin(geom="text", binwidth=1, aes(label=..count..), vjust=-0.5)

Thank you! Eva

Upvotes: 1

Views: 2716

Answers (1)

Richard Telford
Richard Telford

Reputation: 9923

You can use ifelse to supress the labels you don't want

set.seed(42)
example <- data.frame(V1 = rpois(20, 10))

ggplot(data=example, aes(x = V1)) + 
  geom_histogram(binwidth = 1,
                 col = "black") + 
  stat_bin(geom = "text", binwidth = 1, 
    aes(label = ifelse(..count.. > 0, ..count.., "")), vjust = -0.5)

Upvotes: 3

Related Questions