繁体   English   中英

为 r 中的每个 facet_wrap 图制作特定的 x 轴标签

[英]Make specific x-axis labels for each facet_wrap graph in r

我正在做文本分析并尝试按年对图表进行分类,但由于每年最常用的前 5 个词不同,x 轴上的标签也不同。 有什么方法可以让我在每个图表下保留特定的标签,而不是让整个 x 轴变得凌乱不堪?

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

我认为您想依靠tidytext库,它有一个名为reorder_within()的 function。 这是一个如何工作的例子。 字体很小,但如果仔细观察,您会发现每个方面的 y 轴都不同。 您没有在帖子中包含您的数据集。 (您应该通过键入dput(head(df))并将 output 复制并粘贴到您的帖子中来执行此操作,以便我们可以使用您的版本。)

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

在此处输入图像描述

另一种选择是单独创建图形并将它们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

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM