ggplot: why is automated text printed in legend's fill?

Viewed 42

How can I remove the a printed within each fill? (and I am also interested in the explanation to why a is appearing in the first place)

enter image description here

I have

> head(p)
  studie n_otte
1      B     N0
2      B    N3b
3      B    N3b
4      B     N0
5      B     N0
6      B    N3b

And

# Colors
colsze = c("#E1B930", "#2C77BF","#E38072","#6DBCC3", "grey40", "black", "#8B3A62")
# Data and plot
p %>% as_tibble() %>% 
  mutate(nystudie=as.character(studie),
         n_otte=as.factor(n_otte)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n_otte) %>%
  ggplot(aes(nystudie, n, color = n_otte, fill= n_otte, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9), vjust=0, fontface=2) +
  scale_fill_manual(values = alpha(colsze, .2), 
                    name="") + 
  scale_color_manual(values = colsze, 
                     name="") + 
  guides(fill = guide_legend(nrow = 1)) + theme(legend.position="top")

Yielding this plot

enter image description here

I tried adding

+ guides(fill = guide_legend(nrow = 1, override.aes = list(fill = alpha(colsze,0.5), color = colsze, lwd = .8)))  

But received an error:

[[<-.data.frame(*tmp*, i, value = c("#E1B93080", "#2C77BF80",  : 
  replacement have 7 rows, data have 6

I know the colsze has seven colors specified, I just simply assume that data containing 6 values would pick the initial 6 colors.

> colsze
[1] "#E1B930" "#2C77BF" "#E38072" "#6DBCC3" "grey40"  "black"   "#8B3A62"

Data

p <- structure(list(studie = c("B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B"), n_otte = structure(c(1L, 6L, 6L, 1L, 1L, 
6L, 1L, 6L, 2L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 3L, 1L, 6L, 1L, 1L, 
2L, 1L, 1L, 1L, 1L, 4L, 5L, 1L, 4L, 5L, 6L, 1L, 1L, 5L, 1L, 1L, 
6L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 6L, 6L, 6L, 1L, 6L), .Label = c("N0", 
"N1", "N2a", "N2b", "N2c", "N3b"), class = "factor")), row.names = c(NA, 
-50L), class = "data.frame")
2 Answers

Try this. You use to have that kind of issues because of aes() definitions. As you have options like fill enabled, geom_text() use to take all of them and show in legends. One option is to use the argument show.legend that allows hiding undesired elements in legends. Here the code:

library(tidyverse)
# Colors
colsze = c("#E1B930", "#2C77BF","#E38072","#6DBCC3", "grey40", "black", "#8B3A62")
# Data and plot
as_tibble(p) %>% 
  mutate(nystudie=as.character(studie),
         n_otte=as.factor(n_otte)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n_otte) %>%
  ggplot(aes(nystudie, n, color = n_otte, fill= n_otte, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9),
            vjust=0, fontface=2,show.legend = F) +
  scale_fill_manual(values = alpha(colsze, .2), 
                    name="") + 
  scale_color_manual(values = colsze, 
                     name="") + 
  guides(fill = guide_legend(nrow = 1)) + theme(legend.position="top")

Output:

enter image description here

From ggplot v3.2 we can also simply insert key_glyph = draw_key_blank into our geom_text call:

p %>% as_tibble() %>% 
  mutate(nystudie=as.character(studie),
         n_otte=as.factor(n_otte)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n_otte) %>%
  ggplot(aes(nystudie, n, color = n_otte, fill= n_otte, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1)) +
  geom_text(aes(label = n), key_glyph = draw_key_blank,
            position = position_dodge2(0.9), vjust=0, fontface=2) +
  scale_fill_manual(values = alpha(colsze, .2), 
                    name="") + 
  scale_color_manual(values = colsze, 
                     name="") + 
  guides(fill = guide_legend(nrow = 1)) + 
  theme(legend.position="top")

enter image description here

Related