geom_bar():绘制总观测值中子组的频率

geom_bar(): plotting the frequency of a subgroup out of total observations

我对R比较陌生,想问一下: 我有一个包含 2 列的数据框 (my.data):“PHENO”是一个具有两个级别(1 或 2)的因子,“bins”是数字(1 到 10 之间的自然数)。我正在尝试绘制 PHENO==2 与 bin 的频率(以百分比表示),其中 100% 是观测总数(级别 1+2)。

这就是我所做的,但 100% 并不是所有的观察结果:

ggplot(data = subset(my.data, PHENO == 2)) + 
  geom_bar(mapping = aes(x = as.factor(bins), y = ..prop.., group = 1), stat = "count") +
  scale_y_continuous(labels = scales::percent_format(), limits = c(0,0.15)) +
  geom_hline(yintercept = 0.05, linetype="dashed", color = 'blue', size = 1) + 
  annotate(geom = "text", label = 'Prevalence 5%', x = 1.5, y = 0.05, vjust = -1, col = 'blue') +

此外,我尝试在条形图上添加频率标签,但没有成功:

geom_text(aes(label = as.factor(bins)), position=position_dodge(width=0.9), vjust = -0.25)

非常感谢你的帮助。

这是你需要的吗?

df %>% 
  group_by(PHENO, bins) %>% 
  count(PHENO) %>% 
  ungroup() %>% 
  mutate(Percent=n/sum(n)*100) %>% 
  filter(PHENO=="2") %>% #select PHENO 2 here in order to keep 100% of all observations
  ggplot(aes(y=Percent, x=bins))+
  geom_col()+
  geom_hline(yintercept = 5, linetype="dashed", color = 'blue', size = 1)+
  geom_text(aes(label = as.factor(bins)), position=position_dodge(width=0.9), vjust = -0.25)

出于说明目的,我使用了这个模拟数据,当然它可能与您的不对应:

df <- structure(list(PHENO = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L), .Label = c("1", 
"2"), class = "factor"), bins = c(1, 2, 4, 5, 7, 8, 9, 5, 2, 
3, 6, 9, 10, 5, 6, 6, 6, 4)), class = "data.frame", row.names = c(NA, 
-18L))

结果: