2017-04-04 4 views
7

「」など「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を追加する必要があります。上記OganMの答えと同じデータとメソッドを使用して

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: 1つの観察だけの グループの美意識を調整する必要がありますか? – Sathish

+0

説明で編集しました – OganM

2

、あなたは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")