Why this code is not right statically in ggplot to get percentage in y-axis?

Viewed 84

I have this data, and I want to get percentage in y-axis.

structure(list(sb_1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("0", "x"), class = "factor"), 
    sb_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "0", class = "factor"), sb_3 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "b", class = "factor"), 
    sb_4 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("0", "c"), class = "factor"), wave = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("h", 
    "j"), class = "factor")), row.names = c(NA, 12L), class = "data.frame")

This the code I have used:

nn%>%
  pivot_longer(cols = starts_with("sb_")) %>%
  filter(value != 0) %>%
  unite(sb_, name, value) %>%
  group_by(wave) %>%
  mutate(wave_total = n()) %>%
  group_by(sb_, .add = TRUE) %>%
  mutate(sb_pct = 100 * n() / wave_total) %>%
  ggplot(aes(x = factor(sb_, levels = str_sort(unique(sb_), numeric = TRUE)), y = sb_pct)) +
    geom_bar(aes(fill = wave), stat = "identity", position = position_dodge(preserve = "single")) +
    xlab("sb") +
    ylab("percent")

And the outcome is that : ![1]

And the result should be different because for instance for the first column, there was no zero and all is the outcome.

  sb_1 sb_2 sb_3 sb_4 wave
1     0    0    b    0    h
2     0    0    b    0    j
3     0    0    b    0    h
4     0    0    b    c    j
5     0    0    b    c    h
6     0    0    b    c    j
7     x    0    b    c    h
8     x    0    b    c    j
9     x    0    b    c    h
10    x    0    b    c    j
11    x    0    b    c    h
12    x    0    b    c    j

So please help me why is not correct?

1 Answers

I can't tell why your code isn't correct, but I tried a different way and it seems to work as expected:

n <- structure(list(sb_1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("0", "x"), class = "factor"), 
    sb_2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "0", class = "factor"), sb_3 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "b", class = "factor"), 
    sb_4 = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("0", "c"), class = "factor"), wave = structure(c(1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("h", 
    "j"), class = "factor")), row.names = c(NA, 12L), class = "data.frame")

n <- pivot_longer(n, cols = starts_with("sb_"))
n$wave_and_name <- as.factor(paste(n$wave,n$name, sep="_"))
n <- as.data.frame(table(filter(n, value != 0)$wave_and_name) / table(n$wave_and_name) * 100)
n$wave <- substr(n$Var1, 1, 1)
n$name <- substr(n$Var1, 3, 6)

ggplot(n, aes(x=name, y=Freq)) +
  geom_bar(aes(fill = wave), stat="identity",position = position_dodge()) +
  xlab("sb") + 
  ylab("percent")

plot

Related