ggplot2散点图多图例元素控制:IA表型样式调整
调整ggplot散点图IA表型标记样式
需求:将IA表型的标记设置为蓝色、黄色、红色外圈,中心白色的圆形。
修改后的代码
t_chart2 <- ggplot(chart_data, aes(x = CD8.Density.Stroma.mm, y = CD8.Density.Tumor.mm, label = Sample)) + scale_x_log10()+ scale_y_log10()+ # 用shape=21实现可独立设置外圈和填充色的圆形,colour绑定IA表型,fill固定白色 geom_point(aes(colour = IA.Phenotype), shape = 21, fill = "white", size = 3, stroke = 2) + scale_color_manual(values = c("Blue", "Orange", "Red")) + theme_bw() + guides(colour = guide_legend(override.aes = list(shape = 21)))
关键改动说明
- shape=21:这是ggplot中专门支持分离设置外圈(stroke)和填充色(fill)的圆形标记,完美匹配需求中的“外圈颜色+白色中心”样式。
- 固定
fill="white":确保所有IA表型标记的中心为白色。 - 保留
colour与IA.Phenotype的绑定:通过scale_color_manual指定蓝、橙、红三种外圈颜色,对应不同的IA表型类别。
如果需要保留原代码中Indication的形状区分,可调整为以下代码:
t_chart2 <- ggplot(chart_data, aes(x = CD8.Density.Stroma.mm, y = CD8.Density.Tumor.mm, label = Sample)) + scale_x_log10()+ scale_y_log10()+ geom_point(aes(shape = Indication, colour = IA.Phenotype), fill = "white", size = 3, stroke = 2) + scale_shape_manual(values = c(21, 22, 23, 24, 25)) + scale_color_manual(values = c("Blue", "Orange", "Red")) + theme_bw() + guides(fill = guide_legend(override.aes = list(shape =21)), colour = guide_legend(override.aes = list(shape =21)))
内容的提问来源于stack exchange,提问作者fgootkind
相关产品推荐
相关产品推荐

