【发布时间】:2019-04-24 20:29:18
【问题描述】:
考虑以下数据帧的 head(10):
它是由这个 dplyr 代码生成的:
Fuller_list %>%
as.data.frame() %>%
select(from_infomap, topic) %>%
add_count(from_infomap) %>%
filter(from_infomap %in% coms_keep) %>%
group_by(from_infomap) %>%
add_count(topic) %>%
top_n(10, nn) %>%
head(10)
“from_infomap”栏中有 36 个不同的社区,“topic”栏中有 47 个不同的主题。按“from_infomap”分组每个社区的主题数量,对于前 5 个社区,如下所示:
我想展示每个社区最常见的 10 个主题,按降序排列。我在这里尝试这样做:
group_by(from_infomap) %>%
add_count(topic) %>%
top_n(10, nn)
但如果我绘制它,它只会返回每个社区的前 1 个主题:
我不确定我做错了什么。根据this 堆栈溢出查询,计数上的加权 top_n(n,wt) 函数应该可以工作,它应该给出按计数加权的前 10 个主题,按社区分组。
如果有人可以提出替代方案或指出我哪里出错了,将不胜感激。对于小屏幕截图,我无法在此处显示整个 data.frame,因为它非常大。
谢谢!
编辑:没有 group_by、add_count 和 top_n 的 dput:
n <- Fuller_list %>%
as.data.frame() %>%
select(from_infomap, topic) %>%
add_count(from_infomap) %>%
filter(from_infomap %in% coms_keep) %>%
group_by(from_infomap)
输入(头(n,10)):
structure(list(from_infomap = c(1L, 1L, 1L, 3L, 3L, 3L, 4L, 4L,
4L, 4L), topic = c("KnysnaFire_thanks_wofire", "Abramjee_caperelief_operationsa",
"Pick_n_Pay", "Plett_heavy_rain_snow", "Disasters_help_call",
"KFM_disasters_discussion", "Pick_n_Pay", "Pick_n_Pay", "Pick_n_Pay",
"Pick_n_Pay"), n = c(30512L, 30512L, 30512L, 6572L, 6572L, 6572L,
5030L, 5030L, 5030L, 5030L)), row.names = c(NA, -10L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "from_infomap", drop = TRUE, indices = list(
0:2, 3:5, 6:9), group_sizes = c(3L, 3L, 4L), biggest_group_size = 4L, labels = structure(list(
from_infomap = c(1L, 3L, 4L)), row.names = c(NA, -3L), class = "data.frame", vars = "from_infomap", drop = TRUE))
通过将此代码添加到前一个块应该可以重现问题:
add_count(topic) %>%
top_n(10,nn) %>%
ungroup() %>%
ggplot(aes(x = fct_reorder(topic,nn),y = nn,fill = from_infomap))+
geom_col(width = 1)+
facet_wrap(~from_infomap, scales = "free")+
coord_flip()+
theme(plot.title = element_text("Central Players"),
plot.subtitle= element_text("Top 10 indegree centrality profiles of the 20 biggest communities.\n Excluding 'starburst' communities."),
plot.caption = element_text("Source: Twitter"))+
theme_few()
Halway-Solution:所以使用@s_t 建议的summary 方法,我们有以下代码:
Fuller_list %>%
as.data.frame() %>%
add_count(from_infomap) %>%
filter(from_infomap %in% coms_keep) %>%
group_by(from_infomap,topic) %>% # group by the topic and community
summarise(nn = n()) %>% # count the mentioned arguments
top_n(10, nn) %>%
ungroup() %>%
arrange(from_infomap, nn) %>%
ggplot(aes(x = fct_reorder(topic,nn),y = nn,fill = from_infomap))+
geom_col(width = 1)+
facet_wrap(~from_infomap, scales = "free")+
coord_flip()+
theme(plot.title = element_text("Central Players"),
plot.subtitle= element_text("Top 10 indegree centrality profiles of the 20 biggest communities.\n Excluding 'starburst' communities."),
plot.caption = element_text("Source: Twitter"))+
theme_few()
哪个是各个社区中正确的top_n(10)。出于所有实际目的,该图现在显示了正确的数据。唯一剩下的问题是,排列不是按 每个社区 的 desc 顺序对各种主题进行排序,而是按整体排序。小问题,如果主题可以按社区安排,只会改善 aes。
【问题讨论】:
-
你能发布一些有用的数据(不是图片)吗?
-
@s_t 我添加了指向可下载代码示例的链接。我希望这就是你要找的东西? drive.google.com/file/d/128R9Vgjd2QsFwHf0M5Yi8ltli2dsDsrJ/…
-
@snoram,我想使用 dput 来提供代码示例,但是数据集非常大(很多变量),在这里实际上无法查看。我提供了一个 1000 行示例的链接,我希望这是为了?
-
@s_t 当然,我在下面添加了 dput(head()) 的输出。那是你要找的吗?抱歉,我还没有这方面的经验。