如何在R的geom_dotplot中减小Y轴点间距并实现点堆叠?
R绘制抗体滴度DotPlot的点堆叠优化方案
问题背景
用R绘制以**抗体滴度(Y轴,范围1:20至1:640)**为纵轴、**采样地点(X轴)**为横轴的DotPlot时,无法缩小Y轴上的点间距,尝试log2缩放未达到预期的紧凑点堆叠效果,期望实现点在对应滴度值处集中堆叠的样式。
示例数据
structure(list(AMOSTRA = c("A11", "A17", "A19", "A20", "A21", "A23", "A24", "A27", "A30", "A32", "A33", "A35", "A77", "A91", "A111", "A193", "A200", "A206", "A216", "A291", "A292", "A304", "A324", "A336", "A339", "A340", "A351", "A353", "A354", "A372", "A375", "A385", "A389", "A392", "A396", "H059", "H074", "H081", "H104", "H107", "H109", "H114", "H134", "H163", "H166", "H186", "H208", "H221", "H222", "H256"), NEUTRALIZAÇÃO = c("Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Neutralizante", "Não neutralizante", "Não neutralizante", "Não neutralizante", "Neutralizante", "Neutralizante", "Não neutralizante", "Neutralizante"), TITULO = c(80, 40, 640, 640, 40, 160, 160, 160, 80, 20, 640, 160, 320, 640, 320, 320, 640, 640, 640, 40, 160, 320, 640, 640, 80, 320, 640, 160, 80, 160, 320, 640, 320, 320, 640, 640, 640, 640, 640, 160, 160, 320, 320, 80, 80, 40, 160, 640, 80, 640), LOCAL = c("Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Coari", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus", "Manaus"), ROUND = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -50L), na.action = structure(c(`2` = 2L, `4` = 4L, `6` = 6L, `8` = 8L, `10` = 10L, `12` = 12L, `14` = 14L, `16` = 16L, `18` = 18L, `20` = 20L, `22` = 22L, `24` = 24L, `26` = 26L, `28` = 28L, `30` = 30L, `32` = 32L, `34` = 34L, `36` = 36L, `38` = 38L, `40` = 40L, `42` = 42L, `44` = 44L, `46` = 46L, `48` = 48L, `50` = 50L, `52` = 52L, `54` = 54L, `56` = 56L, `58` = 58L, `60` = 60L, `62` = 62L, `64` = 64L, `66` = 66L, `68` = 68L), class = "omit"), class = c("tbl_df", "tbl", "data.frame"))
当前代码
data_summary_1= function(y) { data.frame( y = median(y), ymin = quantile(y, 0.25), ymax = quantile(y, 0.75) ) }
figure_3_median = ggplot(dados, aes(x = LOCAL, y = TITULO, fill = LOCAL)) + geom_dotplot(binaxis = 'y', stackdir = 'center', stackratio = 1.0, dotsize = 0.8) + scale_fill_manual(values = c("#FAD510","#A16928")) + stat_summary(fun.data = data_summary_1, geom = "pointrange", color = 'black') + scale_y_continuous(trans = 'log2', breaks = c(40,160,640), labels = c("40","160","640")) + theme_classic() figure_3_median
当前生成图表

期望效果
实现点在对应滴度值处紧凑堆叠,所有同滴度的点集中排列,无过大垂直间距。
优化方案
问题核心是geom_dotplot默认分箱逻辑在对数轴上适配不佳,未明确绑定滴度的离散值。通过以下调整解决:
优化后代码
# 将滴度转换为有序因子,固定离散水平 dados$TITULO_fct = factor(dados$TITULO, levels = c(20,40,80,160,320,640)) # 适配因子类型的统计函数 data_summary_1= function(y) { y_num = as.numeric(as.character(y)) data.frame( y = median(y_num), ymin = quantile(y_num, 0.25), ymax = quantile(y_num, 0.75) ) } # 绘制优化后的DotPlot figure_3_median_optimized = ggplot(dados, aes(x = LOCAL, y = as.numeric(TITULO_fct), fill = LOCAL)) + geom_dotplot(binaxis = 'y', stackdir = 'center', stackratio = 0.7, dotsize = 0.8, binwidth = 0.6) + scale_fill_manual(values = c("#FAD510","#A16928")) + stat_summary(fun.data = data_summary_1, geom = "pointrange", color = 'black') + scale_y_continuous(trans = 'log2', breaks = c(20,40,80,160,320,640), labels = c("20","40","80","160","320","640")) + labs(y = "抗体滴度") + theme_classic() figure_3_median_optimized
关键调整说明
- 因子转换:将连续滴度转为有序因子,强制点仅在指定的离散滴度水平上堆叠,避免分箱错位。
- binwidth参数:设置为0.6,匹配因子转换后的数值间隔,确保每个滴度值对应一个独立分箱。
- stackratio调整:从1.0改为0.7,缩小堆叠点的垂直间距,实现紧凑效果。
- 完整刻度:添加所有存在的滴度值到Y轴刻度,保证图表信息完整。
内容的提问来源于stack exchange,提问作者Gabriel Caetano Scachetti
相关产品推荐
相关产品推荐

