沿着facet_grid ggplot中特定面的自由轴

iezvtpos  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(128)

我想做两件事:
首先,也是最重要的一点是,要让y轴随outcome_type2变量变化,因为它们的值域都非常不同,所以"C"有自己的值域,"Z"有自己的值域,"SS"有自己的值域。
第二件事是调整列的间距,这样相同的组之间就有一点空间--三个"C"列靠得很近,在它们和"Z"之间有一点额外的空白,"Z"和"SS"之间也是这样,只是为了在这三组之间多加一点区分。
我尝试在outcome_type2而不是outcome_type上修改刻面,但没有效果。
这是当前的基本代码,从技术上讲运行良好,但正如您将看到的,让它们都使用相同的Y轴确实会淹没"Z"和"SS"面板。

ggplot(dtest, aes(x = var2, y = avg2, fill = var2)) +
  geom_bar(stat = "identity",
           width = 1) +
  facet_grid(wave ~ forcats::fct_relevel(outcome_type, "CT", "CI", "CE", "FZ", "MZ", "PSS", "CSS"),
             scales = "free_y",
             space = "free_y") +
  theme_minimal() +
  theme(legend.position = "none")
dtest <- structure(list(outcome_type = c("CT", "CT", "CT", "CI", "CI", 
"CI", "CE", "CE", "CE", "FZ", "FZ", "MZ", "MZ", "PSS", "PSS", 
"CSS", "CSS", "CT", "CT", "CT", "CI", "CI", "CI", "CE", "CE", 
"CE", "FZ", "FZ", "MZ", "MZ", "PSS", "PSS", "CSS", "CSS"), wave = c("Wave 1", 
"Wave 2", "Wave 3", "Wave 1", "Wave 2", "Wave 3", "Wave 1", "Wave 2", 
"Wave 3", "Wave 2", "Wave 3", "Wave 2", "Wave 3", "Wave 1", "Wave 3", 
"Wave 1", "Wave 3", "Wave 1", "Wave 2", "Wave 3", "Wave 1", "Wave 2", 
"Wave 3", "Wave 1", "Wave 2", "Wave 3", "Wave 2", "Wave 3", "Wave 2", 
"Wave 3", "Wave 1", "Wave 3", "Wave 1", "Wave 3"), var2 = c("Skipped", 
"Skipped", "Skipped", "Skipped", "Skipped", "Skipped", "Skipped", 
"Skipped", "Skipped", "Skipped", "Skipped", "Skipped", "Skipped", 
"Skipped", "Skipped", "Skipped", "Skipped", "Attended", "Attended", 
"Attended", "Attended", "Attended", "Attended", "Attended", "Attended", 
"Attended", "Attended", "Attended", "Attended", "Attended", "Attended", 
"Attended", "Attended", "Attended"), avg2 = c(30.21, 20.88, 25.43, 
7.68, 8.26, 7.89, 11.15, 8, 5.99, 1.64, 0.43, 0.6, 0.77, 0.01, 
-0.09, -0.2, -0.01, 24.01, 19.98, 29.04, 9.82, 12.41, 12.99, 
14.35, 11.01, 10, 2.36, 2.3, 1.51, 0.91, -0.23, -0.35, -0.17, 
-0.14), outcome_type2 = c("C", "C", "C", "C", "C", "C", "C", 
"C", "C", "Z", "Z", "Z", "Z", "SS", "SS", "SS", "SS", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "Z", "Z", "Z", "Z", "SS", 
"SS", "SS", "SS")), class = c("spec_tbl_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -34L), spec = structure(list(
    cols = list(outcome_type = structure(list(), class = c("collector_character", 
    "collector")), wave = structure(list(), class = c("collector_character", 
    "collector")), var2 = structure(list(), class = c("collector_character", 
    "collector")), avg2 = structure(list(), class = c("collector_double", 
    "collector")), outcome_type2 = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))

vaj7vani

vaj7vani1#

一种选择是为每组面板创建单独的绘图,然后使用patchwork将它们粘合在一起。这样做可以自动为每组面板获得“自由”缩放,并且每个面板组也有一个(且只有一个)轴。
为此,首先添加一个group列到您的数据中,它可以用于按面板组分割您的数据集。另外,为了方便起见,我使用了一个绘图函数,它也删除了前两组面板的y轴带文本,作为重要的一步,complete s每个数据集,以便wave的所有组合,outcome_typevar2存在于每个子数据集中。

library(ggplot2)
library(patchwork)
library(magrittr)

dtest$group <- dplyr::case_when(
  grepl("SS$", dtest$outcome_type) ~ "SS",
  grepl("Z$", dtest$outcome_type) ~ "Z",
  TRUE ~ "C"
)
dtest$group <- factor(dtest$group, c("C", "Z", "SS"))

plot_fun <- function(.data) {
  remove_facet <- if (unique(.data$group) %in% c("C", "Z")) {
    theme(strip.text.y = element_blank())
  }

  .data$outcome_type <- forcats::fct_relevel(
    .data$outcome_type,
    "CT", "CI", "CE", "FZ", "MZ", "PSS", "CSS"
  )

  .data |>
    tidyr::complete(outcome_type, wave = unique(dtest$wave), var2) %>%
    ggplot(aes(x = var2, y = avg2, fill = var2)) +
    geom_bar(
      stat = "identity",
      width = 1
    ) +
    facet_grid(wave ~ outcome_type) +
    theme_minimal() +
    remove_facet
}

dtest_split <- split(dtest, dtest$group)

lapply(dtest_split, plot_fun) %>%
  wrap_plots() +
  plot_layout(widths = c(3, 2, 2), guides = "collect") &
  labs(x = NULL, y = NULL, fill = NULL) &
  theme(axis.text.x = element_blank())
#> Warning: 4 unknown levels in `f`: FZ, MZ, PSS, and CSS
#> Warning: 5 unknown levels in `f`: CT, CI, CE, PSS, and CSS
#> Warning: 5 unknown levels in `f`: CT, CI, CE, FZ, and MZ
#> Warning: Removed 4 rows containing missing values (`position_stack()`).
#> Removed 4 rows containing missing values (`position_stack()`).

mrwjdhj3

mrwjdhj32#

这里是一个解决方案,我们首先识别那些avg2〈5,然后制作两个 Dataframe 的列表,并为每个 Dataframe 绘制相应的图:

library(tidyverse)
require(gridExtra)

my_list <- dtest %>% 
  pivot_longer(c(contains("type"))) %>% 
  mutate(value = fct_relevel(value, "CT", "CI", "CE", "FZ", "MZ", "PSS", "CSS")) %>% 
  arrange(value) %>% 
  mutate(x = ifelse(avg2 <5, 1, 0)) %>% 
  group_split(x)
  

plot1 <- ggplot(my_list[[1]], aes(x = var2, y = avg2, fill = var2))+
  geom_col()+
  facet_grid(wave ~ value) +
  theme_minimal() +
  theme(legend.position = "none",
        strip.text.y = element_blank()
        )

plot2 <- ggplot(my_list[[2]], aes(x = var2, y = avg2, fill = var2))+
  geom_col()+
  facet_grid(wave ~ value)+
  theme_minimal() +
  theme(legend.position = "none")+
  labs(y="")

  

grid.arrange(plot1, plot2, ncol=2)

相关问题