0

I have the following dataset:

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")

At the moment I am plotting it in this way:


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

The well-know issue with this is that facet_wrap plots the labels alphabetically. Instead, I would like to plot it on the basis of the highest frequency of the latest week (second week in this case, W2).

Is there anyone who could help me achieve it?

Thanks!

Rollo99
  • 1,601
  • 7
  • 15

2 Answers2

1

You can extract the correct order in which you want to show the data by keeping data only for last week and arranging it in decreasing order of the frequency.

Change the factor levels of original data and plot.

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")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • thank you both! Both answers work well on my case, I just awarded the user with less points. Thanks again! – Rollo99 Jul 30 '21 at 06:59
1

This I think? gives what you are looking for.

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")

I'm not totally happy about how 'latest week' is determined here but will work up to W9.