带有刻面网格的哑铃图错误,无法创建并排图

Posted

技术标签:

【中文标题】带有刻面网格的哑铃图错误,无法创建并排图【英文标题】:Error in dumbbell plot with facet grid, unable to create a side by side plot 【发布时间】:2021-12-25 08:57:07 【问题描述】:

我尝试将哑铃图与分面网格并排绘制,但出现了一些有趣的错误。

错误:至少一层必须包含所有分面变量:Trial_typeSession.

绘图丢失Trial_typeSession. 缺少第 1 层 Session. 缺少第 2 层 Session. 缺少第 3 层 Session. 缺少第 4 层 Session. 缺少第 5 层 Trial_typeSession. 缺少第 6 层 Session. 运行rlang::last_error() 以查看错误发生的位置。 另外:警告信息: 1:在 min(x) 中:min 没有非缺失参数;返回 Inf 2:在 max(x) 中:max 没有非缺失参数;返回-Inf 3:在 min(x) 中:min 没有非缺失参数;返回 Inf 4:在 max(x) 中:max 没有非缺失参数;返回 -Inf
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(tidyverse)
  library(dumbbell)
 dumbbell::dumbbell(data10A, id = "conditon2", key = "Trial_type", 
          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( Trial_type ~ Session., 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"))
 

【问题讨论】:

【参考方案1】:

问题是dumbbell::dumbbell 在通过ggplot2 构建哑铃图之前做了一些数据预处理。除其他外,在此步骤中删除了不必要的列,即仅保留 dumbbell 的参数中指定的列,而在您的情况下包括您的 Session 列的所有其他列都将被删除。因此,您无法通过Session 进行分面。

一种解决方法是为您的数据添加一个新的键列,这只是Trial_typeSession 的交互。然后,您可以将此键列传递给 dumbbell 之外的 key 参数,并可用作分面变量:

library(ggplot2)
library(dumbbell)

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

由reprex package (v2.0.1) 于 2021 年 11 月 14 日创建

【讨论】:

你能帮我用 facet_nested 编辑代码吗?谢谢 嗨鲁本。 TBMK 通过dumbbell 或我在答案中显示的解决方法是不可能的facet_nested 需要两个单独的列。如果您需要特定的刻面布局,我建议您使用 geom_segmentgeom_point 从头开始​​构建哑铃图。 不错,去掉右边图上的y轴标签来创造空间怎么样

以上是关于带有刻面网格的哑铃图错误,无法创建并排图的主要内容,如果未能解决你的问题,请参考以下文章

将 Scanpy 小提琴图并排绘制的问题

Tableau 图表大全24之哑铃图

如何从 R 中的数据子集创建两个并排图? [复制]

R语言ggplot2使用geom_line函数geom_point函数可视化哑铃图并对哑铃图进行排序(reorder dumbbell plot)

R数据可视化16:哑铃图

编辑 emmeans 的箭头图的刻面文本