2015-04-06 63 views
0

我正在繪製一些測試中的一些數據,這些數據是在進行干預之前重複進行的。因此,我想用誤差線將結果繪製在同一圖上。由於兩次試驗的誤差線相交,難以破譯,因此我只想爲後期測試向上繪製誤差線,而僅在預測試時向下繪製誤差線。 這可能在ggplot2?ggplot2中的一個方向錯誤欄

示例代碼:

library("ggplot2") 
set.seed(1) 
dat <- data.frame(Trial = c(rep("Pre",9),rep("Post",9)), 
        Time = rep.int(seq(0,120,15),2), 
        Insulin = c(rnorm(9,15,2),rnorm(9,22,2)), 
        Insulin_sd = c(rnorm(18,3,1))) 


p3 <- ggplot(data = dat, aes(x = Time, y = Insulin, group = Trial))+ 
    geom_errorbar(aes(ymin = Insulin - Insulin_sd, ymax = Insulin + Insulin_sd,linetype = Trial), width = 4) + 
    geom_line(aes(linetype = Trial)) + 
    geom_point(aes(shape= Trial, fill = Trial), size = 2) + 
    scale_shape_manual(values=c(21,24),guide = guide_legend(reverse = TRUE)) + 
    scale_fill_manual(values=c("black","white"),guide = guide_legend(reverse = TRUE)) + 
    scale_linetype_manual(values = c("solid","dashed"),guide = guide_legend(reverse = TRUE)) + 
    scale_y_continuous(limits= c(0,35)) 

(抱歉 - 不能夠上載的圖像)

任何幫助不勝感激。

+0

如何有關修改「錯誤」數據集,所以一個個有下部和上部的其他所有零所有零? –

+0

嗨,卡爾,你的意思是類似的過程Curt的解決方案下面?謝謝。 – sp202

+0

是的,這就是我要做的。 –

回答

3

靠近你想要的一種方法是根據你現有的數據使用布爾邏輯手動設置誤差條的最大值和最小值。

dat$min <- dat$Insulin - (dat$Trial=='Pre')*dat$Insulin_sd 

dat$max <- dat$Insulin + (dat$Trial=='Post')*dat$Insulin_sd 

p3 <- ggplot(data = dat, aes(x = Time, y = Insulin, group = Trial)) + 
     geom_errorbar(aes(ymin=min, ymax=max, linetype = Trial), width = 4) + 
     geom_line(aes(linetype = Trial)) + 
     geom_point(aes(shape= Trial, fill = Trial), size=2) + 
     scale_shape_manual(values=c(21,24), guide=guide_legend(reverse = TRUE)) + 
     scale_fill_manual(values=c("black","white"), guide= 
      guide_legend(reverse = TRUE)) + 
     scale_linetype_manual(values = c("solid","dashed"), 
      guide=guide_legend(reverse = TRUE)) + 
     scale_y_continuous(limits= c(0,35)) 

PNG of p3 produced by above code

+0

謝謝。這是一個很好的工作,我沒有想到。我將進一步探索完整的數據集。 – sp202

+0

我對我的完整數據集很適合。謝謝! – sp202