How to remove 0 count labels from histogram in ggplot?

Hi all

I am making a Shiny app. I tried to make a histogram with the count visualised per bin.

This is the output:

This is the code for the ggplot histogram:

ggplot(data = df_TAT_filtered,
             mapping = aes(TAT)) +
        geom_histogram( binwidth = 5, boundary = 0, color="blue", fill="light blue") +
        stat_bin(binwidth = 5, center = 2.5, geom="text", aes(label=..count..), vjust = -1) +
        xlim(0, input$upper_limit) +
        labs(title="amount of samples and their TAT",x="TAT (in classes of 5 mins)", y = "amount of samples") +
        theme(plot.title = element_text(hjust = 0.5)) +
        theme(axis.text.x = element_text(angle = 90)) +
        scale_x_continuous(limits = c(0, input$upper_limit), breaks = seq(0, input$upper_limit, 5))

I want to remove the 0 count labels. I tried to adapt like this:

stat_bin(binwidth = 5, center = 2.5, geom="text", aes(label= if_else(..count.. > 0), ..count.., ""), vjust = -1)

I get this error (even when I add "stat = count", the problem remains):

How can I solve this?

Thanks in advance!

change this part to

aes(label=after_stat(if_else (condition = count>0,
                              as.character(count),
                              "")))
1 Like

That is correct! Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.