gpt4 book ai didi

r - 使用 dplyr 选择前 n 个组,然后绘制其他变量

转载 作者:行者123 更新时间:2023-12-02 14:53:31 25 4
gpt4 key购买 nike

我有一个数据集,我试图通过计算一个类别来选择前 n 个,然后使用数据集中的其他变量进行绘图——基本上是前 n 个聚合的一个级别,但需要返回到在 ggplot 中绘制的完整数据。

所以在下面的问题中,我想要两个最常见的 examName,然后根据 year 的数量绘制和 facetwrap 它们。

ap <- 
tribble(
~year, ~examName,
2014, "Statistics",
2015, "Statistics",
2016, "Statistics",
2016, "Statistics",
2016, "Statistics",
2016, "Statistics",
2017, "Statistics",
2017, "Statistics",
2017, "Statistics",
2017, "Statistics",
2017, "Statistics",
2013, "Macroeconomics",
2013, "Macroeconomics",
2014, "Macroeconomics",
2015, "Macroeconomics",
2016, "Macroeconomics",
2016, "Macroeconomics",
2016, "Macroeconomics",
2016, "Macroeconomics",
2016, "Macroeconomics",
2017, "Macroeconomics",
2017, "Macroeconomics",
2017, "Macroeconomics",
2017, "Macroeconomics",
2017, "Macroeconomics",
2017, "Macroeconomics",
2013, "Calculus",
2014, "Calculus",
2015, "Calculus",
2016, "Calculus",
2017, "Calculus",
2017, "Psychology",
2017, "Psychology",
2017, "Psychology",
2017, "Psychology",
2017, "Psychology",
2018, "Psychology",
2018, "Psychology")


ap_top <- ap %>%
count(examName, sort = TRUE) %>%
head(2) %>%
inner_join(ap, by = "examName") %>%
select(-n)

ap_top %>%
count(examName, year) %>%
ggplot(aes(x = year, y = n, group = examName)) +
geom_line() +
facet_wrap(~ examName)

我的想法是让我的前 n 个,然后 inner_join 回到原始数据集。然后使用它进行绘图;本质上是使用内部连接作为过滤器。

我知道有更好的方法可以做到这一点,我希望有一个更优雅的解决方案!我洗耳恭听!给出的示例数据集(抱歉太长了)。

最佳答案

您不需要 inner_join() 我只会在单独的语句中确定前两项考试,然后对其进行过滤。

top_exams <- count(ap, examName) %>% 
top_n(2, n) %>% pull(examName)

ap %>%
filter(examName %in% top_exams) %>%
count(year, examName) %>%
ggplot(aes(x = year, y = n, group = examName)) +
geom_line() +
facet_wrap(~ examName)

关于r - 使用 dplyr 选择前 n 个组,然后绘制其他变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54260789/

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