向facet_wrap添加次要标题

bmp9r5qi  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(174)

我有一个由分布在两个位置(即NorthSouth)的八个站点(即ABC ... H)组成的数据框。我已经使用facet_wrap()为每个站点制作了一个数字,但是,我想添加一个额外的列标题来表示站点的位置。我该如何做呢?
示例数据

library(ggplot2)
library(dplyr)

set.seed(123)

df <- data.frame(matrix(ncol = 4, nrow = 24))
colnames(df)[1:4] <- c('location','site','x','y')
df$location <- rep(c('North','North','North','South','South','South'),4)
df$site <- c('A','A','A','E','E','E','B','B','B','F','F','F',
             'C','C','C','G','G','G','D','D','D','H','H','H')
df$x <- rep(seq(0,12,4),6)
df$y <- rnorm(24,50,20)
df

示例图(缺少二级标题)

df %>%
  mutate(across(site, factor, levels = c('A','B','E','F',
                                         'C','D','G','H'))) %>%
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks = seq(0,12,3),
                     limits = c(0,12)) +
  scale_y_continuous(breaks = seq(0,max(df$y),5)) +
  theme_bw() +
  facet_wrap(~site, nrow = 2)

这里有一个类似的SO问题(link here),但是,当已经调用了scale_x_continuous()函数时,我无法使它工作,并且不清楚该答案如何与同一轴上的多个标题一起工作。
下面是我正在寻找的输出示例。请注意,df$location是辅助x轴标题,左边两列是North站点,右边两列是South站点。

jm81lzqq

jm81lzqq1#

根据akrun的答案,可以通过在strip_nested()中将相应的元素设置为空来隐藏一个条带,不过我还没有找到删除冗余空间的方法。

library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)
library(dplyr)

set.seed(123)

df <- data.frame(matrix(ncol = 4, nrow = 24))
colnames(df)[1:4] <- c('location','site','x','y')
df$location <- rep(c('North','North','North','South','South','South'),4)
df$site <- c('A','A','A','E','E','E','B','B','B','F','F','F',
             'C','C','C','G','G','G','D','D','D','H','H','H')
df$x <- rep(seq(0,12,4),6)
df$y <- rnorm(24,50,20)
df %>%
  mutate(across(site, factor, levels = c('A','B','E','F',
                                         'C','D','G','H'))) %>%
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks = seq(0,12,3),
                     limits = c(0,12)) +
  scale_y_continuous(breaks = seq(0,max(df$y),5)) +
  theme_bw() +
  facet_manual(
    vars(location, site), design = "ABEF\nCDGH",
    strip = strip_nested(
      text_x = list(element_text(), element_blank())[c(1,1,2,2,rep(1, 8))],
      background_x = list(element_rect(), element_blank())[c(1,1,2,2,rep(1, 8))]
    ))

reprex package(v2.0.1)于2023年1月5日创建

efzxgjgh

efzxgjgh2#

要按自己的意愿设置每个子标题的样式,可以执行以下操作:

library(ggh4x)

# Create blank headers for a dummy variable that we will use for rows
outer_rect <- list(element_blank(), element_blank())
outer_text <- list(element_blank(), element_blank())

# Create black headers for first row of location strips, blank otherwise
loc_rect <- list(element_rect(fill = "black"), element_rect(fill = "black"),
                 element_blank(), element_blank())
loc_text <- list(element_text(colour = "white", size = 14),
                 element_text(colour = "white", size = 14),
                 element_blank(), element_blank())

# Create 8 normal strips for the letter headers
final_rect <- elem_list_rect(fill = rep("gray", 8))
final_text <- elem_list_text(colour = rep("black", 8))

现在,我们创建一个伪变量,为字母分配适当的facet行。

df %>%
  mutate(across(site, factor, levels = c('A','B','E','F',
                                         'C','D','G','H'))) %>%
  # This generates a dummy variable (1 for first row, 2 for second row)
  mutate(rownum = site %in%  c('C','D','G','H') + 1) %>%
  ggplot(aes(x = x, y = y)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(breaks = seq(0,12,3),
                     limits = c(0,12)) +
  scale_y_continuous(breaks = seq(0,max(df$y),5)) +
  theme_bw() +
  facet_nested_wrap(rownum~location + site, nrow = 2,
                    strip = strip_nested(
                      background_x = c(outer_rect, loc_rect, final_rect),
                      text_x = c(outer_text, loc_text, final_text)
                    )) +
  theme(panel.spacing.y = unit(-10, "mm"))

相关问题