r - 在 R 中使用 ggplot2 绘制地铁图

标签 r ggplot2

我想使用 ggplot2 中的流式细胞术数据绘制地铁 map 或与此图片类似的内容:

FlowCytometrySubwayMap

我可以提供清晰可见的完整路径的可重复数据:

fullPopulationPaths = 
 c("root",                                                                                                                            
   "/Single Cells",                                                                                                                   
   "/Single Cells/Live",                                                                                                              
   "/Single Cells/Live/CD45 int CD123+",                                                                                              
   "/Single Cells/Live/CD45 int CD123+/CD45 int CD123+ DR-CD38+ Basophils",                                                           
   "/Single Cells/Live/CD45+",                                                                                                        
   "/Single Cells/Live/CD45+/Lymphocytes",                                                                                            
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR+",                                                                                  
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR+/CCR7+TCRgd+",                                                                      
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR+/CCR7-",                                                                            
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR+/CCR7-CD45RA++TCRgd+",                                                              
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR-",                                                                                  
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR-/CD3+ CD56-" ,                                                                      
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR-/CD3+ CD56-/CD4+",                                                                  
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR-/CD3+ CD56-/CD4+/CD4+CD45RA++CCR7+"   ,                                             
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR-/CD3+ CD56-/CD4+/CD4+CD45RA++CCR7+/naive CD4+" ,                                    
   "/Single Cells/Live/CD45+/Lymphocytes/CD3+ TCR-/CD3+ CD56-/CD4+/CD4+CD45RA-CCR7+")

我尝试使用这篇文章的答案中的代码 how to create a subway map visualisation of my data但它对我的数据不起作用。

最佳答案

我可能会将数据转换为 igraph 并使用 ggraph 进行绘图。最困难的部分是定位标签:

library(igraph)
library(tidygraph)
library(ggraph)

do.call(rbind, lapply(strsplit(fullPopulationPaths, "/")[-(1:2)], 
        function(x) {
        x <- x[nzchar(x)]
        x[length(x) + c(-1, 0)]
        })) |>
  as.data.frame() |>
  graph_from_data_frame() |>
  as_tbl_graph() |>
  ggraph() +
  geom_edge_diagonal(aes(colour = factor(from)), width = 2) +
  geom_node_point(size = 5) +
  geom_node_point(size = 2, colour = "white") +
  geom_node_text(aes(label = name), 
                 nudge_y = c(0.3, -0.5, 0, 0, -0.5, -0.3, 0, 0, 0,
                             -0.5, -0.5, -0.5, 0, 0, -0.5, -0.5),
                 nudge_x = c(0, 0, -1, 1, 0, 0, -1, 1, 1, 
                             0, 0, 0, 1, -1, 0, 0)/10,
                 hjust = c(0.5, 0.5, 1, 0, 0.5, 1, 1, 0, 0, 0.5, 0.5,
                           0.5, 0, 1, 0.8, 0.5),
                 fontface = "italic") +
  scale_edge_color_discrete(guide = "none") +
  theme_void()

enter image description here

关于r - 在 R 中使用 ggplot2 绘制地铁图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75816166/

相关文章:

r - 使用[[和向量对数据框进行索引时,日期列将被强制转换为数字

r - 我想要连续有多个输入的 Shiny UI

r - 在 ggplot 中获取刻度中断位置

r - 不透明的传奇盒子

html - 使用 R 从雅虎财经抓取头条新闻和日期

R 添加具有预定义模式的新列

R用ggmap对象叠加geom_polygon,空间文件转换

r - 如何在ggplot中使用R的原生管道和占位符

R ggplot2 - 具有来自第三个连续变量的渐变颜色的geom_smooth

r - ggplot2 geom_area 重叠而不是堆叠