2

When labeling columns on a geom_col plot I can label them with this code:

geom_text(aes(x = Date, y = Cases, label = Cases), vjust = -0.5)+

some values are zero for Cases, and I would like not to show those.

John
  • 45
  • 6
  • 1
    Try `geom_text(aes(x = Date, y = Cases, label = ifelse(Cases !=0, Cases, "")), vjust = -0.5)` – stefan Jul 03 '20 at 13:04
  • ifelse, works, thank you very much, learning more every day! – John Jul 03 '20 at 13:44
  • 1
    Does this answer your question? [ggplot2: display text labels from one group only](https://stackoverflow.com/questions/40658754/ggplot2-display-text-labels-from-one-group-only) – stefan Jul 03 '20 at 19:17

1 Answers1

2

You could also filter the data within the call of geom_label():

structure(list(x = c("A", "B", "C", "D", "E", "F"), n = c(3, 
5, 10, 7, 5, 0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))

ggplot2::ggplot(df, aes(x = x, y = n, label = n))+
  geom_col(stat = "identity")+
  geom_label(data = df %>% dplyr::filter(n > 0), vjust = 0.5)

enter image description here

mabreitling
  • 608
  • 4
  • 6