FeaturePlot_scCustom作图

2024-07-13 03:36:03 浏览数 (4)

分享一下昨天在分析单细胞数据的时候遇到的一个问题:

Seurat object里面有12个samples, 我想呈现A gene在12个samples里面的表达情况。FeaturePlot()是首选,然后12个samples挤在一起,根本看不到图,只呈现x-y轴和挤在一起的title。于是我用了ncol 参数,但是这个参数在featurePlot() 根本无效。这里需要make一下,出现这样的问题还是应该去Seurat官网找寻说明书。。。。在浪费一些时间之后,我发现了有团队推出了FeaturePlot_scCustom() function, 请移步 https://samuel-marsh.github.io/scCustomize/index.html for details。不知道是不是Seurat团队。

根据官网的介绍,代码如下:

代码语言:r复制
# Show specific features in each sample.
p <- FeaturePlot_scCustom(seurat_object = object1, 
                          features = "TIGIT", 
                          split.by = "sample.origin",
                          # num_columns = 6,
                          combine = FALSE)  
p1 <- lapply(p,function(x){
  x   theme(
    plot.title = element_text(size = 6),     # Title size
    axis.title = element_text(size = 5),     # Axis title size
    axis.text = element_text(size = 4),      # Axis text size
    legend.title = element_text(size = 5),   # Legend title size
    legend.text = element_text(size = 4)     # Legend text size
  )
})

# Combine adjusted plots into one figure
combined_plot <- wrap_plots(p1,ncol = 6,guides = 'collect') &
  theme(legend.position = 'bottom', 
        legend.key.size = unit(0.5, 'cm'), 
        plot.title = element_text(size = 6),
        axis.title = element_text(size = 5),
        axis.text = element_text(size = 5),
        legend.title = element_text(size = 4),
        legend.text = element_text(size = 3))

print(combined_plot)

上述的代码可以成功的解决我的需求,即12个samples 分成 2 rows * 6 columns来呈现 gene的表达。但是最大的问题就是缺了最后一个sample...为此,又花了近3h的时间来troubleshooting。

p is a patchwork object, we need to check the length of p (returns 12) and whether each of element is accessible。很显然,12个sample中的最后一个sample没法accessible。一行代码就可以解决这个问题:

代码语言:r复制
plots <- lapply(1:length(p), function(i) p[[i]])

所以最终的代码如下:

代码语言:r复制
# Show specific features in each sample.
p <- FeaturePlot_scCustom(seurat_object = object1, 
                          features = "TIGIT", 
                          split.by = "sample.origin",
                          # num_columns = 6,
                          combine = FALSE)  # Set combine to FALSE to keep individual plots
plots <- lapply(1:length(p), function(i) p[[i]])
length(plots)  # Should be 12
p1 <- lapply(plots,function(x){
  x   theme(
    plot.title = element_text(size = 6),     # Title size
    axis.title = element_text(size = 5),     # Axis title size
    axis.text = element_text(size = 4),      # Axis text size
    legend.title = element_text(size = 5),   # Legend title size
    legend.text = element_text(size = 4)     # Legend text size
  )
})

length(p1)
# Combine adjusted plots into one figure
combined_plot <- wrap_plots(p1,ncol = 6,guides = 'collect') &
  theme(legend.position = 'bottom', 
        legend.key.size = unit(0.5, 'cm'), 
        plot.title = element_text(size = 6),
        axis.title = element_text(size = 5),
        axis.text = element_text(size = 5),
        legend.title = element_text(size = 4),
        legend.text = element_text(size = 3))

print(combined_plot)

总结:

需要了解每一个对象的结构和内容,才能及时的发现每个function/command做了哪些事情。

多使用 str(), class(), length()。

0 人点赞