简体   繁体   中英

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. (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.)

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. 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).

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(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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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