简体   繁体   English

R ggplot2:绘图中的点,线和误差线不一致重叠

[英]R ggplot2: Points, lines and error bars in plot overlap inconsistently

I need to plot some things, my data is available in a previous post , which helped me quite a lot in dealing with ggplot2. 我需要绘制一些东西,我的数据在以前的文章中可用,这对我处理ggplot2有很大帮助。 (In that previous post, I needed the following plot binned by quantiles of variable miht.binned , but as it comes to formal layout, I start with a more simple plot without that binning variable.) (在上miht.binned文章中,我需要下面的图由变量miht.binned的分位数进行分miht.binned ,但对于正式布局,我从一个没有该分箱变量的更简单的图开始。)

My current best plot version is: 我当前最好的情节版本是: 在此处输入图片说明

I am almost satisfied, except that the points, lines and bars seem to overlap inconsistently. 除了点,线和条的重叠似乎不一致之外,我几乎感到满意。 Especially, when you look at the negative group at T1, it is overridden by the black line which doesn't look very professional. 特别是,当您查看T1处的负数组时,它会被看起来不太专业的黑线覆盖。 Is there a way, to work around this and make overlaps being in a more consistent, meaningful way? 有没有一种方法可以解决此问题,并以更一致,更有意义的方式进行重叠? The issue is independent of export format (image, pdf). 该问题与导出格式无关(图像,pdf)。

The code for this plot: 该图的代码:

pd <- position_dodge(.2)
ggplot(MyData, aes(x=time, y=ias, colour=GROUP, group=GROUP, 
                                 shape=GROUP)) + 
  stat_summary(fun.data = "mean_se", geom="errorbar",position=pd) +
  stat_summary(fun.y="mean", geom="point", size=8,position=pd) + 
  stat_summary(fun.y="mean", geom="line",position=pd) + 
  scale_x_discrete(breaks=c("0", "1", "2"), labels=c("T0", "T1", "T2"))+
  coord_cartesian(ylim=c(2, 7)) + 
  scale_y_continuous(breaks=seq(2, 6, 2)) +
  scale_color_manual(values=c("gray30", "gray50","gray70"),name  ="Gruppe",
                                           breaks=c("baseline", "negative", "neutral"),
                                            labels=c("Baseline", "Attend-negative", "Attend-neutral")) +
  scale_shape_discrete(name  ="Gruppe",
                       breaks=c("baseline", "negative", "neutral"),
                       labels=c("Baseline", "Attend-negative", "Attend-neutral")) +
  theme(
    panel.grid.major.y = element_line(colour = "gray80", size = NULL, linetype = NULL,  # horizontale Linien
                                      lineend = NULL)
    ,panel.grid.minor.y = element_line(colour = "gray90", size = NULL, linetype = NULL,
                                       lineend = NULL)
    ,panel.grid.major.x = element_blank()           # vertikale Linien
    ,panel.grid.minor.x = element_blank()
    ,legend.background = element_rect(fill = "white", colour = "white") # Legende 
    ,legend.key = element_rect(fill = "white", colour = "white")
    ,panel.background = element_rect(fill = "white", colour = "white", size = NULL, # Panel Hintergrund
                                     linetype = NULL)
    ,axis.line = element_line(colour = "black", size=.5)
    ,axis.ticks.x = element_line(colour = "black", size=.5)
    ,axis.ticks.y = element_line(colour = "black", size=.5)
    ,axis.ticks.length =  unit(0.5, "cm")
    ,axis.ticks.margin =  unit(.3, "cm")
    ,axis.title.x = element_text(family = NULL, face = "bold", size = 11,vjust=0.1)
    ,axis.title.y = element_text(family = NULL, face = "bold", size = 11,vjust=0.1)
    ,axis.text=element_text(colour="black")
    ,legend.title = element_text(family = NULL, face = "plain", size = 11)
    ,legend.text = element_text(family = NULL, face = "plain", size = 9)
  ) +
  xlab("Messzeitpunkt")+
  ylab("State-KA (M)") 

Just change the order of stat_summary() calls in the order you need (they plotted in actual plot in the order they are called in your code) - first errorbars, then lines and points as last. 只需按照需要的顺序更改stat_summary()调用的顺序即可(它们按照在代码中调用的顺序在实际绘图中绘制)-第一个误差线,然后是线和点。

  + stat_summary(fun.data = "mean_se", geom="errorbar",position=pd) +
  stat_summary(fun.y="mean", geom="line",position=pd) + 
  stat_summary(fun.y="mean", geom="point", size=8,position=pd) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM