r - 制作带有与聚类和基因表达相关的彩色侧边栏的 DotPlot
问题描述
我想制作一个 DotPlot,它添加了一个额外的特征,用于将特征基因链接到它们从中提取的集群。
我可以使用 dittoDotPlot 轻松生成标准 DotPlot:
p1 <- dittoDotPlot(Liver, top10_filter$gene, group.by = "DefClustering") + coord_flip() + theme(axis.text.x.bottom = element_text(colour = my_palette_Rui_colors))
我找到了一个网站(https://davemcg.github.io/post/lets-plot-scrna-dotplots/#lets-glue-them-together-with-cowplot),他们尝试添加标签,在这种情况下是关联集群与 CellType (我想将基因与 CellType 相关联)
labels <- ggplot(gene_cluster %>%
mutate(`Cell Type` = Group,
cluster = factor(cluster, levels = v_clust$labels[v_clust$order])),
aes(x = cluster, y = 1, fill = `Cell Type`)) +
geom_tile() +
scale_fill_brewer(palette = 'Set1') +
theme_nothing() +
xlim2(dotplot)
现在我已经制作了这张表,可以很容易地将每个基因与我感兴趣的集群相关联。
head(top10_filter)
# A tibble: 6 × 7
# Groups: gene [6]
p_val avg_log2FC pct.1 pct.2 p_val_adj cluster gene
<dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 0 3.19 0.957 0.004 0 Arteries GJA5
2 0 1.83 0.783 0.005 0 Arteries LMO7
3 1.42e-303 2.73 0.652 0.003 4.59e-299 Arteries COL8A1
4 1.72e-282 2.67 0.783 0.006 5.56e-278 Arteries SDC1
5 1.63e-262 3.11 0.522 0.002 5.26e-258 Arteries NPR3
6 1.76e-228 2.68 0.826 0.01 5.69e-224 Arteries NRG1
那么如何使用 ggplot 来获得我想要的情节呢?现在我可以在右侧生成条,但颜色与每个集群无关,它们是按比例分配的。
labels <- ggplot( top10_filter %>%
mutate(cluster,
),
aes(x = 1, y = cluster, fill = top10_filter$cluster), color = my_palette_Rui_colors) +
geom_tile() +
theme_nothing() +
xlim2(p1)
p1 + labels
我不完全了解示例中集群和细胞类型之间的相关性是如何完成的,也许在我的基因和集群之间不能以同样的方式完成。无论哪种方式,我都不知道如何前进。提前致谢!
解决方案
我认为你在 geom_tile 的正确轨道上。您只需要在那里定义所需的美学。
下面我包含了一个使用 mtcars 数据集的最小示例。不是最漂亮的,但我希望它显示了这个想法。
您只需要确保您的 x 变量是字符类型或因子类型。
如果您希望 geom_tile 在图的右侧,但在图例之前,您可以使用“x = length(unique(data_tmp$cyl))+1”的技巧
data_tmp <- mtcars
data_tmp$type <- rownames(data_tmp)
data_tmp$cyl <- paste0(data_tmp$cyl, "_cylinder")
#type = gene
#cyl = cluster
color <- data_tmp$cyl
plot <- ggplot(data_tmp, aes(y=type, x=cyl)) +
geom_point(aes(size=drat, color = qsec)) +
geom_tile(aes(y=type, x = length(unique(data_tmp$cyl))+1, fill = cyl)) + # creates a bar on the rights side of your data, coloured by your x-variable
scale_fill_manual(values=rainbow(8)) +
theme_classic()
plot
要定义颜色,而不是 Rainbow(x),您还可以使用命名向量,例如:
colours_used <- c("cluster1" = "red", "cluster2" = "blue", "cluster3" = "green")
推荐阅读
- javascript - 用 => 定义的函数如何在 Angular 中被调用?
- django - 在弹性beantalk中设置静态文件
- javascript - 父类的泛型类型 javascript
- python-3.x - 查找列表和列表列表元素之间的部分字符串匹配
- javascript - 如何为引导模式内的 ng-select 添加自动对焦
- javascript - 使用 JavsScript 将宽度设置为跨度不起作用
- api - 如何将相机捕获的图像传递到服务器?
- node.js - 在 Async-Await 中的 Await 处永远停止
- sql - 将 Crystal Reports 连接到 SQL Server 时出现问题
- bash - ffmpeg 将音频混合代码组合成复杂的连接脚本