简体   繁体   English

向每个facet_grid图添加y轴标签

[英]Add y-axis labels to each facet_grid plot

I have a plot that you can simply reproduce with 我有一个情节,你可以简单地复制

library(dplyr)
library(ggplot2)

tibble(x = sample.int(3, 1000, replace = T),
       y = sample(letters[1:3], 1000, replace = T),
       k = sample(LETTERS[4:6], 1000, replace = T)) %>%
  count(x,y,k) %>%
  ggplot(aes(k, n)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  facet_grid(x ~ y)

which gives something like the plot below (depends on random sampling): 如下图所示(取决于随机抽样):

在此处输入图片说明

I want to add y labels to each axis, but still be able to use facet_grid layout. 我想为每个轴添加y标签,但仍然能够使用facet_grid布局。 Mostly beacause I need labels for each row of plots and not above them. 通常是因为我需要为每行图添加标签,而不是在它们上方。 In this case it might sound weird but in my data I have a 5x6 grid of plots and there are 10 labels on y axis. 在这种情况下,听起来可能很奇怪,但是在我的数据中,我有一个5x6的网格图,y轴上有10个标签。 This would simplify readability of the whole plot. 这将简化整个图的可读性。

I want to achieve plot like this: 我想实现这样的情节:

在此处输入图片说明

You can achieve it using facet_wrap , but it changes the layout. 您可以使用facet_wrap来实现,但是会更改布局。

library(dplyr)
library(ggplot2)

tibble(x = sample.int(3, 1000, replace = T),
       y = sample(letters[1:3], 1000, replace = T),
       k = sample(LETTERS[4:6], 1000, replace = T)) %>%
  count(x,y,k) %>%
  ggplot(aes(k, n)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  facet_wrap(~ interaction(x, y), scales = "free_y")

在此处输入图片说明

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

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