删除哑铃图上的 y 刻度标签以创建 space

Remove y-tick labels on a dummbell plot to create space

我在这里得到了关于使用 facet-wrap 绘制哑铃图的帮助,但我想为我的图表创建 space。这是 facet_wrap 的当前图表。所以我只希望 y-ticks 出现一次(即在左边的图上)

data10A <- structure(list(Session = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), conditon2 = structure(c(10L, 
4L, 12L, 6L, 11L, 5L, 1L, 7L, 3L, 9L, 2L, 8L, 10L, 4L, 12L, 6L, 
11L, 5L, 1L, 7L, 3L, 9L, 2L, 8L), .Label = c("CEN_LLL", "CTL_LLL", 
"IPS_LLL", "CEN_RRR", "CTL_RRR", "IPS_RRR", "CEN_RLR", "CTL_RLR", 
"IPS_RLR", "CEN_LRL", "CTL_LRL", "IPS_LRL"), class = "factor"), 
    Trial_type = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
    1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 
    2L), .Label = c("Retention", "Transfer"), class = "factor"), 
    NormalizedJerk_102 = c(1270.168699, 2099.703957, 3259.268053, 
    1152.257445, 3810.890123, 4601.918336, 1792.371775, 1288.768888, 
    2699.08162, 1650.968794, 2018.457394, 6159.567785, 931.350429, 
    1053.84252, 1611.673955, 1034.363607, 5352.195367, 2499.83996, 
    1560.678962, 915.3845866, 1948.757464, 1341.815274, 2113.732859, 
    2051.140838), NormalizedJerk_104 = c(853.7034116, 924.8554548, 
    2268.966702, 675.7160839, 2442.874632, 1603.954653, 1010.111276, 
    794.1752256, 1313.813984, 1197.638788, 1039.577947, 3125.131019, 
    561.2311988, 767.7541159, 1019.744071, 769.6067294, 2232.404471, 
    1292.509181, 884.8343164, 663.0273865, 1230.369444, 717.8466364, 
    1536.027898, 1027.358586), key = c("Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk", "Change in normalized jerk", 
    "Change in normalized jerk")), row.names = c(NA, -24L), class = c("tbl_df", 
"tbl", "data.frame"))
library(ggplot2)
library(dumbbell)
library(ggh4x)

data10A$key <- paste(data10A$Trial_type, data10A$Session, sep = "\n")
dumbbell::dumbbell(data10A, id = "conditon2", key = "key",
                   leg = "Test", 
                   column1 = "NormalizedJerk_102", 
                   column2 = "NormalizedJerk_104", 
                   delt = 1, lab1 = "Pre-test", lab2 = "Post-test", 
                   p_col1 = "black", p_col2 = "grey40", 
                   textsize = 4, segsize = 1.5, 
                   pointsize = 2.5, 
                   title = "Change in Normalized jerk from Pre- to Post-test")  + 
  facet_wrap( ~ key, scales="free", ncol=2) +
  theme(axis.text.x = element_text(size = 12, face = "bold"),
        axis.text.y = element_text(size = 11, face = "bold"),
        legend.position = "right",
        legend.text = element_text(size = 12), 
        legend.title = element_text(size = 14),
        strip.text = element_text(face = "bold", size = 14, color = "black"))

TBMK 仅靠 facet_nested 无法达到您想要的结果。问题是,使用 scales="free" 你最终会得到每个面的“重复”y 尺度。但是,简单地删除 scales="free" 不是一种选择,因为在这种情况下,您最终会得到一个显示每个方面未使用的 condition2 类别的图。

因此,实现您想要的结果的一种选择是为每个 Trial_Type 制作单独的图,然后使用例如将它们粘合在一起patchwork。这将允许您删除 scales="free"。此外,为了利用 facet_nested(_wrap) 我放弃了 dumbbell 并使用 geom_segmentgeom_point 加上 geom_text 从头开始​​构建哑铃图以添加差异标签.

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

make_dumbbell <- function(x, max_x) {
  n_y <- length(unique(x$conditon2))
  
  x$conditon2 <- as.character(x$conditon2)
  
  ggplot(x, aes(y = conditon2, yend = conditon2)) +
    geom_segment(aes(x = NormalizedJerk_102, xend = NormalizedJerk_104), size = 1.5, color = "gray80") +
    geom_point(aes(NormalizedJerk_102, color = "pre"), size = 2.5) +
    geom_point(aes(NormalizedJerk_104, color = "post"), size = 2.5) +
    geom_text(aes(label = scales::number(abs(NormalizedJerk_104 - NormalizedJerk_102), accuracy = .1)),
              x = max_x, hjust = 1, fontface = "bold"
    ) +
    annotate(geom = "text", label = "DIFF", y = n_y + 1, x = max_x, color = "red", fontface = "bold", hjust = 1) +
    scale_y_discrete(expand = expansion(add = c(.6, 1.6))) +
    scale_color_manual(values = c(pre = "black", post = "grey40"), labels = c(pre = "Pre-Test", post = "Post-Test")) +
    facet_nested_wrap(~ Trial_type + Session) +
    theme_bw() +
    theme(
      axis.text.x = element_text(size = 12, face = "bold"),
      axis.text.y = element_text(size = 11, face = "bold"),
      legend.position = "top",
      legend.text = element_text(size = 12),
      legend.title = element_text(size = 14),
      strip.text = element_text(face = "bold", size = 14, color = "black")
    ) +
    labs(x = NULL, y = NULL, color = "Test")
}

max_x <- 1.25 * max(c(data10A$NormalizedJerk_102, data10A$NormalizedJerk_104))

data10A %>%
  split(.$Trial_type) %>%
  lapply(make_dumbbell, max_x = max_x) %>%
  wrap_plots(ncol = 1) + 
  plot_layout(guides = "collect") & 
  theme(
    legend.position = "top"
  )