如何根据最后一次观察而不是字母顺序对情节进行排序?
How to order the plot on the basis of the last observation rather than alphabetic order?
我有以下数据集:
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)的最高频率绘制它。
有没有人可以帮助我实现它?
谢谢!
您可以通过仅保留上周的数据并按频率降序排列来提取要显示数据的正确顺序。
更改原始数据和绘图的因子水平。
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")
这个我觉得?给你正在寻找的东西。
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")
我对这里 'latest week' 的确定方式不太满意,但会一直工作到 W9。
我有以下数据集:
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)的最高频率绘制它。
有没有人可以帮助我实现它?
谢谢!
您可以通过仅保留上周的数据并按频率降序排列来提取要显示数据的正确顺序。
更改原始数据和绘图的因子水平。
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")
这个我觉得?给你正在寻找的东西。
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")
我对这里 'latest week' 的确定方式不太满意,但会一直工作到 W9。