ggplot中分组抖动点添加白色轮廓的问题求助
散点图添加白色轮廓的问题解决
问题描述
需求是给带误差棒的散点图每个点添加白色轮廓以提升辨识度,但组合设置时出现多种问题:报错/无输出、点轮廓无变化、丢失抖动效果(点排成一列)、分组美学失效,单独使用分组、美学元素均正常。
原代码如下:
df=read.csv('data.csv') ## data shaping ## clusters=data.frame() clusters = df[df$Person == '1',] clusters = clusters %>% drop_na(Person) cluster_imaging = clusters %>% drop_na(fourth) cluster_imaging$cluster_SEC = as.factor(cluster_imaging$cluster_SEC) cluster_imaging_NT <- cluster_imaging %>% gather(key="ROIs", value = "standardized_value", first, second, third, fourth, fifth, sixth, seventh ) cluster_imaging_NT = cluster_imaging_NT[c("id","cluster_SEC","ROIs","standardized_value")] cluster_acts_NT = cluster_imaging_NT[0:279,] cluster_conn_NT = cluster_imaging_NT[280:651,] ##### ## scatter plot ## conn_NT = ggplot(cluster_conn_NT, aes(x=ROIs, y=standardized_value, fill = cluster_SEC, color = cluster_SEC)) + #geom_boxplot(alpha = 0.3, notch = FALSE, outlier.color = "black") + ylab("y axis") + ggtitle("Title") + scale_y_continuous(limits=c(-6,4)) + scale_color_manual(values=c("gray","orange")) + geom_jitter(aes(group=cluster_SEC), shape=16, size=3, position=position_jitterdodge()) + geom_point(aes(group=cluster_SEC), shape=21, size=3, fill="white", color="black", position=position_jitterdodge()) + stat_summary(aes(group=cluster_SEC), fun = mean, fun.min = function(x) mean(x) - sd(x)/sqrt(length(x)), fun.max = function(x) mean(x) + sd(x)/sqrt(length(x)), geom = 'errorbar', width = 0.5, size = 1, color="black", position = position_dodge(0.75)) + theme_minimal() + theme( plot.title = element_text(size = 18, face = "bold", hjust = 0.5), axis.title = element_text(size = 14, face = "bold"), axis.text = element_text(size = 12), panel.grid.major = element_blank(), panel.grid.minor = element_blank() ) conn_NT
解决方案
之前的问题核心是同时使用geom_jitter和geom_point两个图层,两者的抖动/偏移位置无法同步,导致点错位或丢失抖动效果。另外,shape=16不支持单独设置轮廓色,需改用支持填充+轮廓分离的形状(比如shape=21)。
修改后的完整代码:
df=read.csv('data.csv') ## data shaping ## clusters=data.frame() clusters = df[df$Person == '1',] clusters = clusters %>% drop_na(Person) cluster_imaging = clusters %>% drop_na(fourth) cluster_imaging$cluster_SEC = as.factor(cluster_imaging$cluster_SEC) cluster_imaging_NT <- cluster_imaging %>% gather(key="ROIs", value = "standardized_value", first, second, third, fourth, fifth, sixth, seventh ) cluster_imaging_NT = cluster_imaging_NT[c("id","cluster_SEC","ROIs","standardized_value")] cluster_acts_NT = cluster_imaging_NT[0:279,] cluster_conn_NT = cluster_imaging_NT[280:651,] ##### ## scatter plot ## conn_NT = ggplot(cluster_conn_NT, aes(x=ROIs, y=standardized_value)) + ylab("y axis") + ggtitle("Title") + scale_y_continuous(limits=c(-6,4)) + # 按分组设置填充色 scale_fill_manual(values=c("gray","orange")) + # 单个geom_jitter实现带白色轮廓的抖动点 geom_jitter(aes(fill = cluster_SEC, group=cluster_SEC), shape=21, size=3, color="white", position=position_jitterdodge(dodge.width=0.75, jitter.width=0.2)) + # 误差棒的偏移宽度和geom_jitter保持一致 stat_summary(aes(group=cluster_SEC), fun = mean, fun.min = function(x) mean(x) - sd(x)/sqrt(length(x)), fun.max = function(x) mean(x) + sd(x)/sqrt(length(x)), geom = 'errorbar', width = 0.5, size = 1, color="black", position = position_dodge(0.75)) + theme_minimal() + theme( plot.title = element_text(size = 18, face = "bold", hjust = 0.5), axis.title = element_text(size = 14, face = "bold"), axis.text = element_text(size = 12), panel.grid.major = element_blank(), panel.grid.minor = element_blank() ) conn_NT
关键修改点:
- 移除重复的
geom_point图层,仅保留geom_jitter,用shape=21实现填充色(分组)+白色轮廓的效果 - 将
color从全局aes中移除,改为在geom_jitter中固定为"white"(轮廓色),fill映射cluster_SEC实现分组颜色 - 统一
position_jitterdodge的dodge.width和stat_summary的position_dodge参数为0.75,确保分组点和误差棒对齐 - 可通过调整
jitter.width参数控制点的抖动分散程度,避免点过度重叠
内容的提问来源于stack exchange,提问作者lisaohhh
相关产品推荐
相关产品推荐

