cmirian
cmirian

Reputation: 2253

ggplot: how to insert a blank geom_col for a group with 0 values

I have

p %>% 
  as_tibble() %>% 
  mutate(nystudie=as.character(studie),
         n.seven=as.factor(n.seven)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n.seven) %>%
  ggplot(aes(nystudie, n, color = n.seven, fill= n.seven, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9), vjust=-0.25, fontface=2, cex=4.5, show.legend = F)

Yielding

enter image description here

But my data comprise six (0-5) and not five (0-4) subgroups.

> table(p$studie,p$n.seven)
   
     0  1  2  3  4  5
  A 39 30  5 28  8  0
  B 44 29  5 29  4  0

The expected output is a black line (or similar) corresponding to the group with 0 values, and with 0 written above. Something like this:

enter image description here

Also, the p$n.seven==5 should also appear in the legend.

p <- structure(list(studie = c("B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B",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L, 3L, 4L), .Label = c("0", 
"1", "2", "3", "4", "5"), class = "factor")), row.names = c(NA, 
-221L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 0

Views: 1769

Answers (1)

Duck
Duck

Reputation: 39605

Try this. You can make a subtle trick by adding new data with bind_rows()in your pipeline. Here the code:

library(tidyverse)
#Code
p %>% 
  as_tibble() %>% 
  mutate(nystudie=as.character(studie),
         n.seven=as.character(n.seven)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n.seven) %>%
  bind_rows(data.frame(nystudie=c('A','all','B'),
                       n.seven=as.character(rep(5,3)),
                       n=0,stringsAsFactors = F)) %>%
  ggplot(aes(nystudie, n, color = n.seven, fill= n.seven, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9), vjust=-0.25, fontface=2, cex=4.5, show.legend = F)

Output:

enter image description here

Or better using the practical advice from amazing @Stefan (kudos to him for such as tip in count()):

#Code
p %>% 
  as_tibble() %>% 
  mutate(nystudie=as.character(studie),
         n.seven=as.factor(n.seven)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n.seven,.drop = F) %>%
  ggplot(aes(nystudie, n, color = n.seven, fill= n.seven, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9), vjust=-0.25, fontface=2, cex=4.5, show.legend = F)

Output:

enter image description here

Upvotes: 2

Related Questions