取消列出嵌套列表并使用 ggplot 绘图

HCA*_*CAI 7 r ggplot2 dataframe data.table

我有一个自己制作的可怕的嵌套列表结构,如下所示:

str(CMaster)
List of 4
 $ :List of 6
  ..$ :List of 5
  .. ..$ :List of 15
  .. .. ..$ : num [1, 1:14] 0.144 0.2 0.256 0.352 0.446 ...
  .. .. ..$ : num [1, 1:47] 0.144 0.2 0.375 0.54 0.694 ...
etc
$ :List of 6
      ..$ :List of 1
      .. ..$ :List of 15
      .. .. ..$ : num [1, 1:14] 0.144 0.2 0.256 0.352 0.446 ...
      .. .. ..$ : num [1, 1:47] 0.144 0.2 0.375 0.54 0.694 ...
Run Code Online (Sandbox Code Playgroud)

结构是固定的,但最后一个 15 个列表可能会达到 150K,我需要尝试绘制这个结构。我想尝试绘制按 List of 4 变量分类的箱线图,用于将 List of 15 的所有数字数据压缩到此示例中的 List of 6 中的每一个。我需要先把它全部取消吗?有没有更简单的方法来制作 data.frame 或 data.table 来保留列表的名称并使它们成为绘图的因素?

dfs <- lapply(CMaster, data.frame, stringsAsFactors = FALSE)
Run Code Online (Sandbox Code Playgroud)

编辑:我添加了示例代码

示例代码(接近真实结构)。

D<-list()
DNSIM<-list()
DTime<-list()
DMaster<-list()

  
for(CC in 1:4){
  for(t in 1:6){
    for(N in 1:5){
    for(i in 1:15){
      
      Dmatrix=runif(15)
      D[[i]]=Dmatrix
    }
    DTime[[t]]=D
    }
    DNSIM[[N]]=DTime
  }
  DMaster[[CC]]=DTime
 }
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

输出

它太大而无法复制,我的组织不允许与 onedrive 共享链接。有什么简单的解决方法吗?

编辑2

tibble(lists = CMaster) %>% 
+   mutate(CleaningType = row_number()) %>% 
+   unnest_longer(lists, indices_to = "TimePoint") %>% 
+   unnest_longer(lists, indices_to = "Replicate") %>%
+   unnest_longer(lists, indices_to = "BehaviourObservation")
# A tibble: 1,800 x 5
   lists                 BehaviourObservation Replicate TimePoint CleaningType
   <list>                               <int>     <int>     <int>        <int>
 1 <dbl[,14] [1 × 14]>                      1         1         1            1
 2 <dbl[,47] [1 × 47]>                      2         1         1            1
 3 <dbl[,11] [1 × 11]>                      3         1         1            1
 4 <dbl[,40] [1 × 40]>                      4         1         1            1
 5 <dbl[,40] [1 × 40]>                      5         1         1            1
 6 <dbl[,34] [1 × 34]>                      6         1         1            1
 7 <dbl[,92] [1 × 92]>                      7         1         1            1
 8 <dbl[,31] [1 × 31]>                      8         1         1            1
 9 <dbl[,5] [1 × 5]>                        9         1         1            1
10 <dbl[,103] [1 × 103]>                   10         1         1            1
# … with 1,790 more rows
Run Code Online (Sandbox Code Playgroud)

因此,我尝试添加另一个子子列表,但现在出现大小不兼容的错误。请问对此有什么想法吗?

tibble(lists = CMaster) %>% 
+   mutate(CleaningType = row_number()) %>% 
+   unnest_longer(lists, indices_to = "TimePoint") %>% 
+   unnest_longer(lists, indices_to = "Replicate") %>%
+   unnest_longer(lists, indices_to = "BehaviourObservation") %>%
+   unnest_longer(lists, indices_to = "sub_sub_observation") 

Error: Can't combine `..1$lists` <double[,14]> and `..2$lists` <double[,47]>.
? Incompatible sizes 14 and 47 along axis 2.
Run `rlang::last_error()` to see where the error occurred.
Run Code Online (Sandbox Code Playgroud)

Bas*_*bo1 4

如果您不介意使用 tidyverse,请在下面找到一些代码来使用 来矩形化您的数据tidyr::unnest_longer。请参阅此处,了解如何使用unnest_longer(以及一般如何将嵌套列表转换为 data.frames)的好教程。

我不确定结果之间有什么区别observationsub_observation以及这个图是否是您真正想要的。

这对于您的大型数据集可能(太)慢。

library(tidyverse)

df <- tibble(lists = DMaster) %>% 
  mutate(facet = row_number()) %>% 
  unnest_longer(lists, indices_to = "boxplot") %>% 
  unnest_longer(lists, indices_to = "observation") %>%
  unnest_longer(lists, indices_to = "sub_observation")
  
df %>% 
  ggplot(aes(boxplot, lists, group = boxplot)) + 
  geom_boxplot() +
  facet_wrap(~ facet)
Run Code Online (Sandbox Code Playgroud)

其中给出了一个包含facet(1到4)、boxplot(1到6)、observation(1到15)、sub_observation(1到15)和lists(您的实际数值)的data.frame,以及以下绘图: