本文介绍了如何将每个方面的总样本量添加到geom_histogram中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将每个方面的样本总数添加到 geom_histogram 中.预期输出如下:

阅读

更新

您也可以尝试:

  iris%>%add_count(.,Species)%>%group_by(Species)%>%mutate(n = ifelse(row_number(n)!= 1,NA,n))%>%ggplot(.,mapping = aes(x = Sepal.Length))+geom_histogram(binwidth = 0.1)+facet_wrap(〜Species)+geom_text(aes(label = n,y = 8),size = 5,vjust = -0.5) 

I want to add total sample size of each facet to geom_histogram.Expect output as below:

After read this post ,I write script as below:

iris %>% 
  ggplot(.,mapping=aes(x=Sepal.Length))+
  geom_histogram(binwidth= 0.1)+
  stat_summary(fun = median, fun.max = length,
               geom = "text", aes(label = after_stat(max)), vjust = -1) +
  facet_wrap(~Species)

But get error:Error: stat_summary requires the following missing aesthetics: y.
How to solve this problem?

解决方案

You can try this:

iris %>% left_join(iris %>% group_by(Species) %>% summarise(N=n()))%>%
  mutate(Label=paste0(Species,' (Sample size = ',N,')')) %>%
  ggplot(.,mapping=aes(x=Sepal.Length))+
  geom_histogram(binwidth= 0.1)+
  facet_wrap(~Label)

It will add a label with sample size to facets:

Update

You can also try:

iris %>% add_count(.,Species) %>% group_by(Species) %>% mutate(n=ifelse(row_number(n)!=1,NA,n)) %>%
  ggplot(.,mapping=aes(x=Sepal.Length))+ 
  geom_histogram(binwidth= 0.1)+ 
  facet_wrap(~Species)+ 
  geom_text(aes(label=n,y=8),size=5,vjust=-0.5)

这篇关于如何将每个方面的总样本量添加到geom_histogram中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:05