如何根据最后一次观察而不是字母顺序对情节进行排序? [复制]

Posted

技术标签:

【中文标题】如何根据最后一次观察而不是字母顺序对情节进行排序? [复制]【英文标题】:How to order the plot on the basis of the last observation rather than alphabetic order? [duplicate] 【发布时间】:2021-10-05 06:19:23 【问题描述】:

我有以下数据集:

df = structure(list(words = c("purchases", "may", "balance", "sheet", 
"balance_sheet", "debt", "policy", "last", "risks", "says", "years", 
"still", "higher", "eurozone", "strategy_review", "need", "growth", 
"germany", "asset", "purchases", "may", "balance", "sheet", "balance_sheet", 
"debt", "policy", "last", "risks", "says", "years", "still", 
"higher", "eurozone", "strategy_review", "need", "growth", "germany", 
"asset"), weeks = c("W1", "W1", "W1", "W1", "W1", "W1", "W1", 
"W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1", "W1", 
"W1", "W2", "W2", "W2", "W2", "W2", "W2", "W2", "W2", "W2", "W2", 
"W2", "W2", "W2", "W2", "W2", "W2", "W2", "W2", "W2"), frequency = c(0.12962962962963, 
0.0555555555555556, 0.037037037037037, 0.037037037037037, 0.037037037037037, 
0.0185185185185185, 0, 0.0740740740740741, 0.0185185185185185, 
0.0740740740740741, 0, 0.037037037037037, 0.0555555555555556, 
0.0185185185185185, 0, 0, 0.0555555555555556, 0.037037037037037, 
0.0185185185185185, 0.0657894736842105, 0.0263157894736842, 0.0263157894736842, 
0.0263157894736842, 0.0263157894736842, 0, 0.0526315789473684, 
0.0789473684210526, 0.0131578947368421, 0.0921052631578947, 0.0394736842105263, 
0.0263157894736842, 0.0131578947368421, 0.0263157894736842, 0.0394736842105263, 
0.0263157894736842, 0.0526315789473684, 0.0263157894736842, 0
)), row.names = c(NA, 38L), class = "data.frame")

目前我正在以这种方式绘制它:


df %>%  
  ggplot(aes(x = weeks, y = frequency, group=1)) +
  geom_line() +
  facet_wrap(~ words, scales = "free") +
   labs(x = NULL, y = "Relative frequency")

众所周知的问题是facet_wrap 按字母顺序绘制标签。相反,我想根据最近一周的最高频率(在本例中为第二周,W2)绘制它。

有没有人可以帮我实现它?

谢谢!

【问题讨论】:

【参考方案1】:

我认为这是?提供您正在寻找的东西。

word.order <- df[df$weeks == max(df$weeks), ]
word.order <- word.order[order(word.order$frequency, decreasing = T), 'words']
df$words <- factor(df$words, levels = word.order, ordered = T)

df %>%  
  ggplot(aes(x = weeks, y = frequency, group=1)) +
  geom_line() +
  facet_wrap(~ words, scales = "free") +
  labs(x = NULL, y = "Relative frequency")

我对这里如何确定“最近一周”并不完全满意,但会一直工作到 W9。

【讨论】:

【参考方案2】:

您可以通过仅保留上周的数据并按频率降序排列来提取要显示数据的正确顺序。

更改原始数据的因子水平并绘图。

library(dplyr)
library(ggplot2)

correct_levels <- df %>%
  mutate(week_num = readr::parse_number(weeks)) %>%
  filter(week_num == max(week_num)) %>%
  arrange(desc(frequency)) %>%
  pull(words)

df$words <- factor(df$words, correct_levels)

ggplot(df, aes(x = weeks, y = frequency, group=1)) +
  geom_line() +
  facet_wrap(~ words, scales = "free") +
  labs(x = NULL, y = "Relative frequency")

【讨论】:

谢谢你们!这两个答案都适用于我的情况,我只是给用户较少的分数。再次感谢!

以上是关于如何根据最后一次观察而不是字母顺序对情节进行排序? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

根据子列表中的第二个元素按字母顺序对列表进行排序,但不区分大小写[重复]

Intellij-IDEA:如何按字母顺序对成员进行排序?

使用插入排序算法按字母顺序排序 c#

如何在 swift 中按字母顺序对 JSON 字符串进行排序?

如何按字母顺序对 DBML 对象进行排序?

如何根据名称而不是索引来订购选择?