ggplot set NA color in geom_col()

I want to create a likert scale grafic with ggplot but i can set the color for NA

  1. Values Example
    tibble_umfrage2_num_plot %>% filter(grepl("Inhalt",key))

A tibble: 58 x 4

Groups: key [12]

key value n per

1 Inhalt1_SQ001 1 5 25
2 Inhalt1_SQ001 2 5 25
3 Inhalt1_SQ001 3 5 25
4 Inhalt1_SQ001 4 3 15
5 Inhalt1_SQ001 NA 2 10
6 Inhalt1_SQ002 1 1 5
7 Inhalt1_SQ002 2 7 35
8 Inhalt1_SQ002 3 4 20
9 Inhalt1_SQ002 4 3 15
10 Inhalt1_SQ002 5 3 15

  1. Code

    tibble_umfrage_num_plot2 %>%
    ggplot(aes(x=key, y=per, fill=factor(value, levels=mysort) )) +
    geom_col() + labs(fill = "Likert", x="Fragen", y="Prozent")+
    geom_text(aes(label=per), position=position_stack(.5)) +
    scale_fill_manual(values=c("5" = "darkgreen", "4"= "green", "3"= "orange","2"= "red","1"= "darkred", na.value="black"))+
    ggtitle("n=20 Studenten, MIS 19/20, Selbsteinschätzung der \n Kompetenzen nach der Vorlesung")+
    coord_flip()

  2. Output

  1. Question
    Why is the NA value still white and not black

best regards
Jürgen

Hi @juergen.schedlbauer: It's hard to make entirely sure without having access to a sample of your data to copy and paste, but it looks like you should be able to fix this by using this:

  scale_fill_manual(
    na.translate = TRUE, 
    na.value = 'black',
    values=c("5" = "darkgreen", "4"= "green", "3"= "orange","2"= "red","1"= "darkred")
    )+

in place of your own version of scale_fill_manual(). Does this work for you?

In the future, you can post a sample of your data here by pasting here between a pair of triple bakcticks (```), like this:

```
<--- paste output of dput(tibble_umfrage_num_plot2 %>% head(50)) here
```

or you could modify the tibble output you did post, like this:

tibble_umfrage_num_plot2 <- 
  tribble(
    ~ row, ~key,~value,~n,~per,
    1,'Inhalt1_SQ001',1,5,25,
    2,'Inhalt1_SQ001',2,5,25,
    3,'Inhalt1_SQ001',3,5,25,
    4,'Inhalt1_SQ001',4,3,15,
    5,'Inhalt1_SQ001',NA,2,10,
    6,'Inhalt1_SQ002',1,1,5,
    7,'Inhalt1_SQ002',2,7,35,
    8,'Inhalt1_SQ002',3,4,20,
    9,'Inhalt1_SQ002',4,3,15,
    10,'Inhalt1_SQ002',5,3,15
  )

which makes it much easier folks who'd like to help.

1 Like

Hi dromano,
thx. Problem is solved.
Sorry for the missing data. I'm very knew in R programming.
Attached my data:

dput(tibble_umfrage_num_plot2 %>% head(50))
structure(list(key = c("Ablaeufe1_SQ001", "Ablaeufe1_SQ001",
"Ablaeufe1_SQ001", "Ablaeufe1_SQ001", "Ablaeufe1_SQ002", "Ablaeufe1_SQ002",
"Ablaeufe1_SQ002", "Ablaeufe1_SQ002", "Ablaeufe1_SQ002", "Ablaeufe1_SQ003",
"Ablaeufe1_SQ003", "Ablaeufe1_SQ003", "Ablaeufe1_SQ003", "Ablaeufe2_SQ001",
"Ablaeufe2_SQ001", "Ablaeufe2_SQ001", "Ablaeufe2_SQ001", "Ablaeufe2_SQ002",
"Ablaeufe2_SQ002", "Ablaeufe2_SQ002", "Inhalt1_SQ001", "Inhalt1_SQ001",
"Inhalt1_SQ001", "Inhalt1_SQ001", "Inhalt1_SQ001", "Inhalt1_SQ002",
"Inhalt1_SQ002", "Inhalt1_SQ002", "Inhalt1_SQ002", "Inhalt1_SQ002",
"Inhalt1_SQ002", "Inhalt1_SQ003", "Inhalt1_SQ003", "Inhalt1_SQ003",
"Inhalt1_SQ003", "Inhalt1_SQ003", "Inhalt1_SQ004", "Inhalt1_SQ004",
"Inhalt1_SQ004", "Inhalt1_SQ004", "Inhalt1_SQ006", "Inhalt1_SQ006",
"Inhalt1_SQ006", "Inhalt1_SQ006", "Inhalt1_SQ006", "Inhalt1_SQ007",
"Inhalt1_SQ007", "Inhalt1_SQ007", "Inhalt1_SQ007", "Inhalt1_SQ007"
), value = c("2", "3", "4", "5", "1", "2", "3", "4", "5", "2",
"3", "4", "5", "2", "3", "4", "5", "2", "4", "5", "1", "2", "3",
"4", NA, "1", "2", "3", "4", "5", NA, "1", "3", "4", "5", NA,
"1", "3", "4", "5", "1", "2", "3", "4", "5", "1", "3", "4", "5",
NA), n = c(1L, 2L, 5L, 12L, 2L, 1L, 6L, 8L, 3L, 1L, 1L, 11L,
7L, 1L, 1L, 4L, 14L, 1L, 5L, 14L, 5L, 5L, 5L, 3L, 2L, 1L, 7L,
4L, 3L, 3L, 2L, 1L, 4L, 3L, 10L, 2L, 1L, 2L, 10L, 7L, 2L, 4L,
6L, 5L, 3L, 2L, 3L, 5L, 9L, 1L), per = c(5, 10, 25, 60, 10, 5,
30, 40, 15, 5, 5, 55, 35, 5, 5, 20, 70, 5, 25, 70, 25, 25, 25,
15, 10, 5, 35, 20, 15, 15, 10, 5, 20, 15, 50, 10, 5, 10, 50,
35, 10, 20, 30, 25, 15, 10, 15, 25, 45, 5)), .Names = c("key",
"value", "n", "per"), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -50L), groups = structure(list(
key = c("Ablaeufe1_SQ001", "Ablaeufe1_SQ002", "Ablaeufe1_SQ003",
"Ablaeufe2_SQ001", "Ablaeufe2_SQ002", "Inhalt1_SQ001", "Inhalt1_SQ002",
"Inhalt1_SQ003", "Inhalt1_SQ004", "Inhalt1_SQ006", "Inhalt1_SQ007"
), .rows = list(1:4, 5:9, 10:13, 14:17, 18:20, 21:25, 26:31,
32:36, 37:40, 41:45, 46:50)), .Names = c("key", ".rows"
), row.names = c(NA, -11L), class = c("tbl_df", "tbl", "data.frame"
), .drop = TRUE))

1 Like

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