gpt4 book ai didi

r - top_n() 没有选择 n

转载 作者:行者123 更新时间:2023-12-05 01:42:40 26 4
gpt4 key购买 nike

目的:按降序绘制前 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) 行之后,输出为:

enter image description here

这表明它不会在 20 时停止。这又是可怕的情节:

enter image description here

最佳答案

#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()

在调用top_n之前只需ungroup()

?top_n 你可以读到:

n number of rows to return. If x is grouped, this is the number of rows per group. Will include more than n rows if there are ties.

关于r - top_n() 没有选择 n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51359078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com