'Make specific x-axis labels for each facet_wrap graph in r

I am doing text analysis and trying to have graphs facet by year, but since the top 5 most frequent words for each year are different, the labels on the x-axis are ganna different. Is there any way that I can keep the specific labels under each graph instead of having one messy overall x-axis?

dat2 <- dat %>% 
  filter(president %in% c("George W. Bush", "Barack Obama")) %>%
  group_by(year) %>%
  unnest_tokens(output = word, input = message)  %>% 
  anti_join(stop_words, by = "word") %>%
  count(word) %>%
  arrange(-n) %>%
  slice(n=1:5)

ggplot(dat2, aes(x = reorder(word, n), y = n)) +
  geom_col() + 
  labs(title = "",
       x = "Word",
       y = "Count") +
  facet_wrap(~year) +
  theme_minimal()

output of plot output of dataset



Solution 1:[1]

I think you want to lean on the tidytext library which has a function called reorder_within(). Here is an example of how that works. The font is small, but if you look close you will see the y-axis is different for each facet. You didn't include your dataset in the post. (You should do that by typing dput(head(df)) and copying and pasting the output into your post so we can work with your version.)

mtcars %>%
  dplyr::select(car, mpg, disp, hp, drat, wt, qsec) %>%
  pivot_longer(names_to = 'names', values_to = 'values', 2:7) %>%
  mutate(car = tidytext::reorder_within(car, values, names)) %>%
  ggplot(aes(x = values, y = reorder(car, values), color = names)) +
  geom_point() +
  labs(title = "Mtcars", subtitle = "Dot Plot") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  theme(plot.subtitle = element_text(hjust = 0.5)) +
  tidytext::scale_y_reordered() +
  facet_wrap(~names, scales = 'free')+
  theme(text = element_text(size=6)) +
  theme(legend.position = 'none')

enter image description here

Another option is to create graphs separately and put them together with patchwork.

one <- mtcars %>%
  dplyr::select(car, origin, mpg, cyl, vs, am, gear, carb) %>%
  tidyr::pivot_longer(names_to = 'names', values_to = 'values', 4:8) %>%
  mutate(names = factor(names)) %>%
  mutate(values = as_factor(values)) %>%
  filter(names == 'am') %>%
  ggplot(aes(x = mpg, y = values)) +
  geom_boxplot(fill = '#e76254') + coord_flip() + facet_wrap(~names)
#
two <- mtcars %>%
  dplyr::select(car, origin, mpg, cyl, vs, am, gear, carb) %>%
  tidyr::pivot_longer(names_to = 'names', values_to = 'values', 4:8) %>%
  mutate(names = factor(names)) %>%
  mutate(values = as_factor(values)) %>%
  filter(names == 'carb') %>%
  ggplot(aes(x = mpg, y = values)) +
  labs(title = "Mtcars", subtitle = "Boxplot Panels") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  theme(plot.subtitle = element_text(hjust = 0.5)) +
  geom_boxplot(fill = '#ef8a47') + coord_flip() + facet_wrap(~names)

three <- mtcars %>%
  dplyr::select(car, origin, mpg, cyl, vs, am, gear, carb) %>%
  tidyr::pivot_longer(names_to = 'names', values_to = 'values', 4:8) %>%
  mutate(names = factor(names)) %>%
  mutate(values = as_factor(values)) %>%
  filter(names == 'cyl') %>%
  ggplot(aes(x = mpg, y = values)) +
  geom_boxplot(fill = '#f7aa58') + coord_flip() + facet_wrap(~names)

four <- mtcars %>%
  dplyr::select(car, origin, mpg, cyl, vs, am, gear, carb) %>%
  tidyr::pivot_longer(names_to = 'names', values_to = 'values', 4:8) %>%
  mutate(names = factor(names)) %>%
  mutate(values = as_factor(values)) %>%
  filter(names == 'gear') %>%
  ggplot(aes(x = mpg, y = values)) +
  geom_boxplot(fill = '#ffd06f') + coord_flip() +
facet_wrap(~names)

five <- mtcars %>%
  dplyr::select(car, origin, mpg, cyl, vs, am, gear, carb) %>%
  tidyr::pivot_longer(names_to = 'names', values_to = 'values', 4:8) %>%
  mutate(names = factor(names)) %>%
  mutate(values = as_factor(values)) %>%
  filter(names == 'vs') %>%
  ggplot(aes(x = mpg, y = values)) +
  geom_boxplot(fill = '#ffe6b7') + coord_flip() + `facet_wrap(~names)`

library(patchwork)
one + two + three + four + five

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1