基于组中出现的图

问题描述 投票:0回答:3

我想制作一个条形图,将条形图表示为整个组的比例,而不是通常的percentage。为了使var“计数”,它仅需要在组中发生一次。例如,在此df中,其中id是分组变量

df <-
tibble(id = c(rep(1, 3), rep(2, 3), rep(3, 3)),
       vars = c("a", NA, "b", "c", "d", "e", "a", "a", "a"))

a条将是:

a = 2/3       # since a occurs in 2 out of 3 groups
b = 1/3    
c = 1/3   
d = 1/3   
e = 1/3
r ggplot2
3个回答
2
投票

如果我对您的理解正确,单线就足够了:

ggplot(distinct(df)) + geom_bar(aes(vars, stat(count) / n_distinct(df$id)))

enter image description here


0
投票

有效答案:

tibble(id = c(rep(1, 3), rep(2, 3), rep(3, 3)),
       vars = c("a", "a", "b", "c", "d", "e", "a", "a", "a")) %>% 
  group_by(id) %>% 
  distinct(vars) %>% 
  ungroup() %>% 
  add_count(vars) %>% 
  mutate(prop = n / n_distinct(id)) %>% 
  distinct(vars, .keep_all = T) %>% 
  ggplot(aes(vars, prop)) +
  geom_col()
© www.soinside.com 2019 - 2024. All rights reserved.