我正在尝试制作一个ggplot散点图,该图会根据提供给函数feature
的{{1}}选项添加三层之一:
tsnePlotSubcluster
如果为feature = c("subcluster" , "area" , "age")
,则添加的图层应标出属于指定子集群的点(单元)。
如果为feature == "subcluster"
,则添加的图层应绘制属于指定子群集的相同图层,但这次由其区域着色。
如果为feature == "area"
,则添加的图层应绘制属于指定子群集的相同图层,但这次由其区域着色。
我可以在3个不同的函数中执行此操作,但是当我尝试使用feature == "age"
语句将它们添加到单个函数中时,出现以下错误:
if
tsne.clust <- tsnePlotSubcluster(subclust = "cluster_2", feature = "area")
Error: Cannot add ggproto objects together.
Did you forget to add this object to a ggplot object?
看起来像这样:(下面是reprex)
df
我尝试合并所有三个选项进行绘制的函数是:
cell.name tSNE_1 tSNE_2 nGene Age area subcluster.merge
18513 TCAGCAATCCCTCAGT_235875 17.1932545 20.9951805 994 25 parietal cluster_23
45195 CACATTTAGTGTACCT_55869 2.0990437 -3.1644088 605 14 motor cluster_16
437 ACTGCTCAGCTGGAAC_60204 14.3391798 5.7986418 919 17 occipital cluster_12-35
47652 TTGAACGAGCGGCTTC_24246 -2.4054652 -5.7217611 617 17 motor cluster_16
3079 CTGAAGTGTCCGAGTC_36162 13.3077568 -9.8810075 2360 19 parietal cluster_10-34
73692 TACGGTATCCACGTTC_43045 -3.9540697 -22.1901588 757 25 insula cluster_19-20-40
78111 ACGGGTCAGGAGTTGC_52675 -8.2138674 -5.6368533 680 14 motor cluster_11
77792 TTGAACGGTCTAGAGG_46399 -4.8505234 -17.3649528 1495 25 insula cluster_19-20-40
80576 ACGAGGACACCCAGTG_43377 4.7608973 12.3166870 652 17 PFC cluster_27
40102 CTAGCCTTCGGATGTT_108090 -26.0839271 -6.0513843 2877 18 occipital cluster_17
75778 GAATGAATCGAACGGA_122697 -0.8466168 -21.6881664 681 25 PFC cluster_19-20-40
64808 CTGGTCTCAGTCCTTC_220448 1.4123929 23.1787489 1275 25 parietal cluster_21
31050 CGATGGCGTCGCCATG_107147 12.7008032 -23.3682646 1457 25 temporal cluster_5-24
40011 AAGGCAGCAAGCCCAC_103547 -15.8308776 -9.0420539 2830 18 occipital cluster_15
23802 TTAGGCACATCGGTTA_224119 25.8490750 5.6472168 2354 25 parietal cluster_7-39
55771 CGGAGTCGTGACGCCT_22310 -0.1658289 9.2474600 920 22 motor cluster_13
62142 TAGAGCTAGGTGACCA_270328 -1.8325109 -12.8780762 2493 25 cingulate cluster_4
85340 AGGTCATCAAGCGATG_108496 -18.5638069 19.3544782 1054 20 motor cluster_21
31185 TGGCCAGGTGCTGTAT_271635 5.3272499 -19.8372034 1557 25 cingulate cluster_29
496 AGAGTGGGTTGTGGCC_10259 11.5646170 11.4089743 1549 18 hippocampus cluster_8
2513 GATCTAGTCCAAGCCG_14125 7.6368712 11.6917014 1756 19 motor cluster_8
52795 TACACGATCAGTCCCT_43422 -0.8565756 12.8355195 1534 20 PFC cluster_13
44355 TCTATTGGTCACAAGG_44401 -21.1689622 -8.1854890 1382 25 insula cluster_1
96327 GATCTAGTCGCTTAGA_232432 -26.6976718 10.3691109 877 25 parietal cluster_3-33
41100 GTTACAGGTATGAAAC_43797 -21.6719857 0.6879885 1489 19 somatosensory cluster_3-33
reprex:
tsnePlotSubcluster <- function(feature = "subcluster", # can be area, age, subcluster
subclust = "cluster_1",
size.grey = 0.25,
size.color = 0.3
) {
# params <- plot.params[[celltype]]
# cluster.colors <- color.values[[celltype]]$i
p <- ggplot(tsne.meta) +
# Plot cells in all other subclusters in grey.
geom_point(data = filter(tsne.meta, ! subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, alpha = nGene),
colour = "grey90", size = size.grey) +
# a) Highlight subcluster:
# Plot cells from selected subcluster in color.
{if(feature == "subclust")
geom_point(data = filter(tsne.meta, subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, color = nGene, alpha = nGene),
size = size.color) +
theme(legend.position = 'none') +
scale_color_viridis_c(option = "plasma", begin = 0.1, end = 0.6)} +
# b) Color subcluster cells by age:
{if(feature == "age")
geom_point(data = filter(tsne.meta, subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, color = Age, alpha = nGene),
size = size.color) +
scale_color_viridis_d(option = "plasma") +
theme(legend.position = 'top')} +
# c) Color subcluster cells by area:
{if(feature == "area")
geom_point(data = filter(tsne.meta, subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, color = area, alpha = nGene),
size = size.color) +
scale_color_viridis_d(option = "viridis") +
theme(legend.position = 'top')} +
labs(title = paste(celltype, "|", subclust)) +
theme(plot.subtitle = element_text(color="grey18", size=11, family="Helvetica", face = "plain", hjust = 0.5),
plot.title = element_text(color="grey18", size=12, family="Helvetica", face = "plain"),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()
)
return(p)
# png(paste0("tSNE_", celltype, "_", subclust,".png"), height = 5, width = 6, units = 'in', res = 300)
# print(p)
# dev.off()
}
答案 0 :(得分:3)
请尝试将层附加到ggplot对象,而不要使用一个大表达式。这样,您可以添加一些有关添加哪些图层的逻辑。请注意,没有tsne.meta,我无法真正进行测试。
tsnePlotSubcluster <- function(feature = "subcluster", # can be area, age, subcluster
subclust = "cluster_1",
size.grey = 0.25,
size.color = 0.3) {
# params <- plot.params[[celltype]]
# cluster.colors <- color.values[[celltype]]$i
p <- ggplot(tsne.meta) +
# Plot cells in all other subclusters in grey.
geom_point(data = filter(tsne.meta, ! subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, alpha = nGene),
colour = "grey90", size = size.grey)
# a) Highlight subcluster:
# Plot cells from selected subcluster in color.
if(feature == "subclust") {
p <- p + geom_point(data = filter(tsne.meta, subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, color = nGene, alpha = nGene),
size = size.color) +
theme(legend.position = 'none') +
scale_color_viridis_c(option = "plasma", begin = 0.1, end = 0.6)
}
# b) Color subcluster cells by age:
else if(feature == "age") {
p <- p + geom_point(data = filter(tsne.meta, subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, color = Age, alpha = nGene),
size = size.color) +
scale_color_viridis_d(option = "plasma") +
theme(legend.position = 'top')
}
# c) Color subcluster cells by area:
else if(feature == "area") {
p <- p + geom_point(data = filter(tsne.meta, subcluster.merge == subclust),
aes(tSNE_1, tSNE_2, color = area, alpha = nGene),
size = size.color) +
scale_color_viridis_d(option = "viridis") +
theme(legend.position = 'top')
}
p <- p + labs(title = paste(celltype, "|", subclust)) +
theme(plot.subtitle = element_text(color="grey18", size=11, family="Helvetica", face = "plain", hjust = 0.5),
plot.title = element_text(color="grey18", size=12, family="Helvetica", face = "plain"),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank()
)
return(p)
# png(paste0("tSNE_", celltype, "_", subclust,".png"), height = 5, width = 6, units = 'in', res = 300)
# print(p)
# dev.off()
}
您可能还想看看programmatic aes