2017-04-04 71 views
7

我想圖線從參考點發散「a」到其它點如「B」,「C」,「d」等,ggplot發散線路用誤差棒

數據:

df <- structure(list(value = c(1.40438297796257, 1.44036790976986, 
1.37704383251482, 1.45355096018748, 1.40847559339844, 1.38860635968641, 
1.43714387291229), group = c("a", "b", "c", "d", "e", "f", "g" 
), low = c(1.38956448514689, 1.40198829989962, 1.33523395978584, 
1.42008027933896, 1.37516232159193, 1.34823916425279, 1.397985577859 
), up = c(1.41920147077825, 1.4787475196401, 1.4188537052438, 
1.487021641036, 1.44178886520494, 1.42897355512002, 1.47630216796558 
), sem = c(0.00757411399256711, 0.0120426947992103, 0.0137959906464809, 
0.00953361452671253, 0.00945315870421568, 0.0130586010600045, 
0.0124407008862053)), .Names = c("value", "group", "low", "up", 
"sem"), row.names = c(NA, -7L), class = "data.frame") 

代碼:

library('ggplot2') 
ggplot(df, aes(x = group, y = value, group = 1)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
       aes(ymin = value - sem, ymax = value + sem), 
       colour="red") + 
    geom_point(shape = 21, size = 4, fill="white") 

當前打印:

enter image description here

預計劇情:

enter image description here

+1

我相信'ggplot'羣體之間在這種情況下 「點連接」。這就是爲什麼你需要重複參考值,如下面OganM的答案。這是爲了在'a'和'b'之間劃一條線,你需要觀察'a = group1'和'b = group1'。類似地,在'a'和'c'之間繪製你需要一個觀察,其中'a = group2'和'c = group2'等。 –

回答

8

不知道你爲什麼這樣做group = 1但你需要的group VAR的線分開。在這裏,我創建了與第一個數據點相同的虛擬數據點,與每個數據點位於同一組。請注意,如果您打算使用透明度,這將導致問題,並需要進一步的擺弄。

df = rbind(df[rep(1,5),],df) 

df$lineGroup = c(1:6,1:6) 

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem), 
        colour="red") + 
    geom_point(shape = 21, size = 4, fill="white") 

enter image description here

透明度問題

如果你

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black",alpha=.3) + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem), 
        colour="red",alpha =.3) + 
    geom_point(shape = 21, size = 4, fill="white") 

你會看到第一點是暗由於多個數據點有

存在

enter image description here

要擺脫這種情況,您需要通過aes控制透明度,並添加一個控制可見性的列。

df$alpha = c('visible', rep('hidden',5), rep('visible',6)) 

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up,alpha= alpha), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem,alpha=alpha), 
        colour="red") + 
    scale_alpha_manual(name='',values = c('visible' = 0.3,'hidden' = 0)) + 

    geom_point(aes(), shape = 21, size = 4, fill="white") 

enter image description here

+0

我使用'group = 1'來避免這個錯誤'geom_path:每個組包含只有一個觀察。你是否需要 調整羣體審美?' – Sathish

+0

在解釋中編輯 – OganM

2

使用相同的數據和方法如上OganM的回答,您可以通過在geom_point使用去欺騙數據集中解決透明度問題。這應該工作:

ggplot(df, aes(x = group, y = value, group = lineGroup)) + 
    geom_line(size = 1) + 
    geom_errorbar(width=.2, size = 1, aes(ymin = low, ymax = up), colour="black") + 
    geom_errorbar(width=.2, size = 1, 
        aes(ymin = value - sem, ymax = value + sem), 
        colour="red") + 
    geom_point(data = df[!duplicated(subset(df,select=-lineGroup)),], 
       shape = 21, size = 4, fill="white") 

enter image description here

數據:

df<-structure(list(value = c(1.40438297796257, 1.40438297796257, 
1.40438297796257, 1.40438297796257, 1.40438297796257, 1.40438297796257, 
1.44036790976986, 1.37704383251482, 1.45355096018748, 1.40847559339844, 
1.38860635968641, 1.43714387291229), group = c("a", "a", "a", 
"a", "a", "a", "b", "c", "d", "e", "f", "g"), low = c(1.38956448514689, 
1.38956448514689, 1.38956448514689, 1.38956448514689, 1.38956448514689, 
1.38956448514689, 1.40198829989962, 1.33523395978584, 1.42008027933896, 
1.37516232159193, 1.34823916425279, 1.397985577859), up = c(1.41920147077825, 
1.41920147077825, 1.41920147077825, 1.41920147077825, 1.41920147077825, 
1.41920147077825, 1.4787475196401, 1.4188537052438, 1.487021641036, 
1.44178886520494, 1.42897355512002, 1.47630216796558), sem = c(0.00757411399256711, 
0.00757411399256711, 0.00757411399256711, 0.00757411399256711, 
0.00757411399256711, 0.00757411399256711, 0.0120426947992103, 
0.0137959906464809, 0.00953361452671253, 0.00945315870421568, 
0.0130586010600045, 0.0124407008862053), lineGroup = c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L)), .Names = c("value", 
"group", "low", "up", "sem", "lineGroup"), row.names = c("1", 
"1.1", "1.2", "1.3", "1.4", "11", "2", "3", "4", "5", "6", "7" 
), class = "data.frame")