如何统计三类癌症散点图各象限内不同标签的数量?
问题描述
我有三张散点图,用于对比两个数值连续列的数据,分别对应三种癌症类型,每行数据均归属某一癌症类型。数据子集如下:
structure(list(cancer_type = c("Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Melanoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Renal Cell Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma", "Urothelial Carcinoma"), Model1 = c(0.144175127148628, 0.145591989159584, 0.0984272509813309, 0.0906868129968643, 0.28544145822525, 0.138114541769028, 0.091837003827095, 0.0904595032334328, 0.211963757872581, 0.163982316851616, 0.0935302376747131, 0.127466395497322, 0.117602989077568, 0.18533518910408, 0.0753359571099281, 0.157020777463913, 0.211388036608696, 0.0914847329258919, 0.177859485149384, 0.137649402022362, 0.240238919854164, 0.10163140296936, 0.128856286406517, 0.1811293810606, 0.145569115877151, 0.108640238642693, 0.157251104712486, 0.141889616847038, 0.0737133473157883, 0.140953287482262, 0.196182891726494, 0.135421812534332, 0.174105599522591, 0.0961336940526962, 0.0573264583945274, 0.0880825147032738), Model2 = c(0.314525783061981, 0.343217849731445, 0.315391361713409, 0.353350460529327, 0.562197327613831, 0.292534917593002, 0.616392850875854, 0.284660279750824, 0.532478809356689, 0.341239869594574, 0.35737070441246, 0.31985279917717, 0.619661331176758, 0.224026560783386, 0.268743008375168, 0.344117254018784, 0.542939126491547, 0.267527014017105, 0.2816281914711, 0.443801760673523, 0.552633106708527, 0.387285768985748, 0.186705753207207, 0.234086975455284, 0.287418365478516, 0.564366817474365, 0.392496168613434, 0.642540812492371, 0.688632488250732, 0.430574655532837, 0.360769122838974, 0.58690744638443, 0.510010659694672, 0.559859037399292, 0.665197253227234, 0.460800766944885), stage = c("STAGE1", "STAGE1", "STAGE2", "STAGE4", "STAGE4", "STAGE4", "STAGE3", "STAGE3", "STAGE1", "STAGE2", "STAGE2", "STAGE1", "STAGE1", "STAGE3", "STAGE4", "STAGE1", "STAGE1", "STAGE1", "STAGE1", "STAGE3", "STAGE2", "STAGE4", "STAGE4", "STAGE1", "STAGE1", "STAGE1", "STAGE2", "STAGE4", "STAGE3", "STAGE3", "STAGE2", "STAGE1", "STAGE1", "STAGE4", "STAGE3", "STAGE2")), class = "data.frame", row.names = c("04d83340b8bd", "122T", "1c2a5ac94492", "1d209304d988", "212T", "24ab7fecc92e", "356T", "379fe8924c51", "39T", "3ec4d3fc8bd1", "3f78044299b5", "4260f878a482", "430T", "43b757f285d8", "49c4c0e12e32", "55cc6edfad7f", "62T", "689be0421d3c", "8237266761ca", "85d99ff60fa1", "9T", "a4d25b70d77c", "a74ac0179106", "ac07fd7297c8", "c0f7a7b642cd", "SAM3cb94b0d5297", "SAM47fc46c3d6be", "SAM4b0175e8db6e", "SAM4b7ea015fd9e", "SAM553c3c35b847", "SAM560f23d6a3ad", "SAM5c139c5c1c4f", "SAM5cc2d9036053", "SAM5cfa1699bdb7", "SAM5d989c86255e", "SAM6157c8f38b72"))
每张散点图包含一条水平线(geom_hline)和一条垂直线(geom_vline),可自行调整位置。图中点的填充色对应stage标签,分为黄色(#E69F00)、蓝色(#56B4E9)、灰色(#999999)三种。需要统计每种癌症类型散点图的四个象限内,各stage标签的点的数量(例如黑色素瘤(Melanoma)图的右下象限仅有1个黄色点,左上象限有2个黄色、1个蓝色、1个灰色点)。
当前绘制散点图的代码如下:
scatterplot_for_models= function(data = data, Model_1 = Model_1, Model_2 = Model_2, x = x, y = y){ ggplot(data,aes(1-data[[Model_2]], data[[Model_1]], fill=stage)) + geom_point(size=4,pch=21) + theme_classic()+ facet_wrap(.~cancer_type)+ scale_fill_manual(values=c('#E69F00', '#56B4E9','#999999','#999999')) + xlab("Model 2") + ylab("Model 1") + geom_hline(yintercept = y,linetype=2)+ geom_vline(xintercept = x,linetype=2) }
实现方案
要完成象限内的数量统计,核心是先给每个数据点标记所属象限,再按癌症类型、象限、stage分组计数,具体步骤如下:
1. 明确象限判断逻辑
注意散点图的x轴是1 - Model2,y轴是Model1,因此需要基于转换后的坐标判断象限:
- 左上象限:
1 - Model2 <= x截距且Model1 >= y截距 - 右上象限:
1 - Model2 > x截距且Model1 >= y截距 - 左下象限:
1 - Model2 <= x截距且Model1 < y截距 - 右下象限:
1 - Model2 > x截距且Model1 < y截距
2. 完整统计代码
使用dplyr包处理数据(未安装的话先运行install.packages("dplyr")):
library(dplyr) # 替换为你实际使用的截距值 x_intercept <- 0.6 # 对应geom_vline的x参数 y_intercept <- 0.15 # 对应geom_hline的y参数 # 生成统计结果 quadrant_stats <- your_data %>% # 计算散点图实际使用的x轴值 mutate(transformed_x = 1 - Model2) %>% # 标记每个点所属象限 mutate(quadrant = case_when( transformed_x <= x_intercept & Model1 >= y_intercept ~ "左上象限", transformed_x > x_intercept & Model1 >= y_intercept ~ "右上象限", transformed_x <= x_intercept & Model1 < y_intercept ~ "左下象限", transformed_x > x_intercept & Model1 < y_intercept ~ "右下象限" )) %>% # 按癌症类型、象限、stage分组计数 group_by(cancer_type, quadrant, stage) %>% summarise(count = n(), .groups = "drop") %>% # 排序结果,方便查看 arrange(cancer_type, quadrant) # 查看统计结果 print(quadrant_stats)
3. 关键说明
- 将代码中的
your_data替换为你的实际数据框名称,x_intercept和y_intercept替换为散点图中使用的截距值。 - 如果需要将统计结果可视化到散点图中,可以通过
geom_text添加标注,或者在facet标题中补充统计信息,核心统计逻辑不变。
内容的提问来源于stack exchange,提问作者Programming Noob
相关产品推荐
相关产品推荐

