You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的ggplot2中结合position_dodge与position_nudge偏移灰色元素

解决方案:组合position_dodge与position_nudge偏移灰色元素

没问题!你可以利用ggplot2的position_compose()函数,把分组需要的position_dodge()(或position_jitterdodge())和偏移需要的position_nudge()组合起来,这样既能保留分组的横向排列逻辑,又能让灰色的抖动点和小提琴图整体向右偏移,避开均值标记和误差棒。

修改后的完整代码

year <- c("1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th","1st","3rd","5th")
status <- c("P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","P","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","Q","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R","R")
performance <- c(31,54,79,44,63,86,40,61,75,40,61,80,33,63,77,40,61,81,46,70,80,35,69,83,41,57,73,48,50,82,41,68,90,45,78,84,41,71,86,58,74,97,40,77,80,60,76,95,57,74,98,48,69,82,53,68,97,50,80,86,47,70,93,45,85,87,43,81,88,59,74,106,47,85,80,65,79,99,60,81,101,58,69,86,59,73,107,57,90,88)
tempdata <- data.frame (year, status, performance)
library(ggplot2)

#Establish the graph's general parameters
ggplot(data=tempdata, aes(x=year, y=performance, linetype=status, shape=status, size=status, color=status, fill=status))+
  #Plot the raw, unaveraged data - 组合jitterdodge和nudge向右偏移
  geom_point(position=position_compose(
    position_jitterdodge(dodge.width=0.9, jitter.height=0, jitter.width=0.06),
    position_nudge(x = 0.2)
  ), size = 3.1,color="black", fill="gray50", alpha=0.3, show.legend=FALSE) +
  #Plot a "violin" - 组合dodge和nudge向右偏移
  geom_violin(fill="gray90", color="gray78", position=position_compose(
    position_dodge(width = 0.9),
    position_nudge(x = 0.2)
  ), alpha=0.1, size=0.9, scale="count", linetype="solid", width=.5, show.legend=FALSE) +
  #Plot the Error Bars
  stat_summary(fun.data=mean_cl_normal, position=position_dodge(.9), geom="errorbar", alpha=1.0, width=0.15) +
  #Plot the mean markers
  geom_point(stat = "summary", fun.y="mean", size=8.0, stroke = 2.4, alpha = 1, position=position_dodge(.9), fill=NA)+
  #Create a list of the particular colors (line colors), fills (the fill colors for shapes), line types, and sizes/thicknesses.
  scale_shape_manual(values=c(21, 24, 22))+
  scale_size_manual(values=c(1.2, 1.2, 1.2)) +
  scale_linetype_manual(values=c("solid", "solid", "solid"))+
  scale_color_manual(values=c("blue", "red", "purple"))+
  labs(x="Year", y="Performance", color="Status:", size="Status:", shape="Status:", fill="Status:", linetype="Status:")+
  theme (panel.background=element_rect(fill = NA),panel.grid.major=element_line(color = NA), axis.line=element_line(size=1, color="black"), axis.ticks=element_line(color="black", size=1),axis.ticks.length=unit(5, "mm"), axis.title.x=element_text(margin = margin(t=20, r=0, b=30, l=0)),axis.title.y=element_text(margin = margin(t=0, r=20, b=0, l=0)),legend.position="bottom",legend.key=element_rect(fill=NA, color=NA, size=0.7),legend.key.width=unit(15, "mm"), text=element_text(size=rel(6.0)),legend.title=element_text(size=rel(4.5)), legend.text=element_text(size=rel(4.5)) )

关键说明

  • position_compose()的作用是按顺序应用多个位置变换:先执行内部的dodge/jitterdodge来处理分组的横向排列,再执行nudge来整体偏移元素。
  • 你可以调整position_nudge(x = 0.2)中的x值(比如0.15或0.25),找到最适合的偏移距离,确保灰色元素和均值标记、误差棒完全不重叠。
  • 均值点和误差棒保持原来的position_dodge(.9)不变,这样它们会留在分组的中心位置,和偏移后的灰色元素形成清晰的区分。

内容的提问来源于stack exchange,提问作者user1113568

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:42:03