r - top_n() 没有选择 n
问题描述
目标:按降序绘制前 20 个国家/地区
问题:在使用该top_n
功能时,它坚持选择所有而不是只选择前20名。
这是我的代码:
#Omit missing values
na.omit(kiva_loans)%>%
#Group by country label
group_by(country_code)%>%
dplyr::count(country_code, sort = TRUE)%>%
top_n(20)%>%
ggplot(aes(reorder(x=country_code,n),y=n))+
geom_col(position="dodge",
color = "black",
fill="purple")+
coord_flip()
在该top_n(20)
行之后,输出为:
这表明它并没有在 20 岁时将其切断。这反过来又是可怕的情节:
解决方案
#Omit missing values
na.omit(kiva_loans)%>%
#Group by country label
group_by(country_code)%>%
dplyr::count(country_code, sort = TRUE)%>%
ungroup() %>% # add this to ungroup
top_n(20)%>%
ggplot(aes(reorder(x=country_code,n),y=n))+
geom_col(position="dodge",
color = "black",
fill="purple")+
coord_flip()
就ungroup()
在你打电话之前top_n
从?top_n
你可以读到这个:
n 要返回的行数。如果 x 被分组,则这是每组的行数。如果有平局,将包括多于 n 行。
推荐阅读
- python - 如何解释这个 Python 数组代码?
- java - 应该可以仅使用键盘导航和编辑 NatTables 的过滤器(不需要鼠标)?
- r - 0.1 +0.1... 10 倍 != 1 在 R
- javascript - 修复了 select2 下拉菜单时 div 无法滚动的问题
- django - 避免在 msgstr 中翻译动态内容
- expression - 表格记录显示在文本框power bi中输入数字的基础上
- r - R中基于分组的条件计算
- android - React-Native 版本 0.60.3 中的版本不匹配
- python - 在 Python 或命令行中监控各种 ip 地址
- ef-code-first - EntityFramework Core 生产迁移