图中从R开始的朝阳图未显示最外圈的所有值

ego6inou  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(124)

我正在尝试获取细胞空间数据,并从幻灯片数据中制作一个朝阳图。下面是我使用的 Dataframe 的基本格式。

structure(list(slide = c("LU095", "LU095", "LU095", "LU095", 
                               "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", 
                               "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", 
                               "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", 
                               "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", 
                               "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", 
                               "LU095", "LU095", "LU095", "LU095", "LU095", "LU095", "LU095"
), stroma_bins = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 
                             5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 
                             8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
                             10L, 10L, 10L, 10L, 10L, 10L, 10L), levels = c("0-10% Stroma", 
                                                                            "10-20% Stroma", "20-30% Stroma", "30-40% Stroma", "40-50% Stroma", 
                                                                            "50-60% Stroma", "60-70% Stroma", "70-80% Stroma", "80-90% Stroma", 
                                                                            "90-100% Stroma"), class = "factor"), cd8_percent_bins = structure(c(1L, 
                                                                                                                                                 1L, 3L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
                                                                                                                                                 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 
                                                                                                                                                 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), levels = c("0-2% CD8+ Cells", 
                                                                                                                                                                                                                 "2-4% CD8+ Cells", "4-6% CD8+ Cells", "6-8% CD8+ Cells", "8-10% CD8+ Cells", 
                                                                                                                                                                                                                 "10-15% CD8+ Cells", "15-20% CD8+ Cells", ">20% CD8+ Cells"), class = "factor"), 
Freq = c(8L, 5L, 1L, 7L, 1L, 7L, 2L, 15L, 4L, 4L, 2L, 15L, 
         4L, 3L, 2L, 12L, 15L, 1L, 4L, 2L, 1L, 1L, 16L, 12L, 8L, 8L, 
         4L, 1L, 3L, 1L, 14L, 4L, 17L, 6L, 9L, 11L, 5L, 2L, 51L, 18L, 
         24L, 24L, 17L, 32L, 21L, 11L)), row.names = c(NA, -46L), class = c("data.table", 
                                                                            "data.frame"))

我在R中使用Plotly,但由于某种原因,它只显示了一个地区的朝阳图的最外层。

这是目前为止我拥有的代码。

fig <- plot_ly(
labels = df2$labels,
parents = df2$parents,
values = df2$values,
type = 'sunburst',
branchvalues = 'total')

fig
h7appiyu

h7appiyu1#

你做了什么来聚合数据以在图中创建数据集,这不是你的问题。但是,我看到你有3个水平,而且你没有使用参数ids。你也没有唯一的 * 子代 * 供Plotly解释。
dput输出的数据开始。
对于根或顶级
slide中的所有数据都是相同的,但是我这样写是为了使它更动态,这将返回一行,因为在最高级别中有一个唯一的值。

d1 <- df2 %>% group_by(slide) %>% 
  summarise(values = sum(Freq)) %>% 
  mutate(ids = slide, parents = "") %>%
  rename(labels = slide) %>% 
  select(ids, parents, labels, values) # all frame same order
# # A tibble: 1 × 4
#   ids   labels values parents
#   <chr> <chr>   <int> <chr>  
# 1 LU095 LU095     435 ""

下一级、中级或第一子级
我将采用完全相同的方法,但不是以slide开头,而是以stroma_bins开头。此外,ids将包含父级和当前级别。

d2 <- df2 %>% group_by(stroma_bins) %>% 
  summarise(values = sum(Freq)) %>% 
  mutate(ids = paste0(stroma_bins, " - ", unique(df2$slide)),
         parents = unique(df2$slide)) %>% 
  rename(labels = stroma_bins) %>% 
  select(ids, parents, labels, values)
# # A tibble: 10 × 4
#    ids                    parents labels         values
#    <chr>                  <chr>   <fct>           <int>
#  1 0-10% Stroma - LU095   LU095   0-10% Stroma        8
#  2 10-20% Stroma - LU095  LU095   10-20% Stroma       6
#  3 20-30% Stroma - LU095  LU095   20-30% Stroma       8
#  4 30-40% Stroma - LU095  LU095   30-40% Stroma       9
#  5 40-50% Stroma - LU095  LU095   40-50% Stroma      25
#  6 50-60% Stroma - LU095  LU095   50-60% Stroma      24
#  7 60-70% Stroma - LU095  LU095   60-70% Stroma      36
#  8 70-80% Stroma - LU095  LU095   70-80% Stroma      53
#  9 80-90% Stroma - LU095  LU095   80-90% Stroma      68
# 10 90-100% Stroma - LU095 LU095   90-100% Stroma    198

下一层有两个父节点,因此 both parents将被包含。它遵循与上两层相同的前提,但是在parents列中,我们需要合并父节点。(我只包含了这个框架的一个示例。)

d3 <- df2 %>% 
  rename(labels = cd8_percent_bins,
         values = Freq) %>% 
  mutate(ids = paste0(labels, " - ", stroma_bins),
         parents = paste0(stroma_bins, " - ", unique(df2$slide))) %>% 
  select(ids, parents, labels, values)
#                                    ids                parents            labels values
#  1:     0-2% CD8+ Cells - 0-10% Stroma   0-10% Stroma - LU095   0-2% CD8+ Cells      8
#  2:    0-2% CD8+ Cells - 10-20% Stroma  10-20% Stroma - LU095   0-2% CD8+ Cells      5
#  3:    4-6% CD8+ Cells - 10-20% Stroma  10-20% Stroma - LU095   4-6% CD8+ Cells      1
#  4:    0-2% CD8+ Cells - 20-30% Stroma  20-30% Stroma - LU095   0-2% CD8+ Cells      7
#  5:    2-4% CD8+ Cells - 20-30% Stroma  20-30% Stroma - LU095   2-4% CD8+ Cells      1
#  6:    0-2% CD8+ Cells - 30-40% Stroma  30-40% Stroma - LU095   0-2% CD8+ Cells      7
#  7:    2-4% CD8+ Cells - 30-40% Stroma  30-40% Stroma - LU095   2-4% CD8+ Cells      2
#  8:    0-2% CD8+ Cells - 40-50% Stroma  40-50% Stroma - LU095   0-2% CD8+ Cells     15
#  9:    2-4% CD8+ Cells - 40-50% Stroma  40-50% Stroma - LU095   2-4% CD8+ Cells      4
# 10:    4-6% CD8+ Cells - 40-50% Stroma  40-50% Stroma - LU095   4-6% CD8+ Cells      4

接下来,将这三个 Dataframe 组合成一个 Dataframe 。

dd <- do.call(rbind, list(d1, d2, d3))

现在数据准备就绪。

plot_ly(dd, parents = ~parents, labels = ~labels, values = ~values,
        ids = ~ids, branchvalues = "total", type = "sunburst")

相关问题