Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

ggplot2 - adding shade to R lineplot denotes standard error

I'm looking for a R or ggplot2 solution for plotting a line with shade denotes standard error. Have been google'd a while without lucky.

line plot with shade denotes SE

Did anyone have similar experience and would like to share would be appreciated.

Sample code I used:

> dat <- read.table('sample',header=TRUE)
> ggplot(dat, aes(x=pos,y=value, colour=type))+geom_line()

The figure I generated:

sample figure

Sample data:

pos value   type
1   1.40685064701   A
2   1.58314330023   A
3   1.74204838899   A
4   1.61736939797   A
5   1.29508580767   A
6   1.09467905031   A
7   1.10472385941   A
8   1.02381316251   A
9   1.30213436484   A
10  1.70752481609   A
11  2.01875034644   A
12  1.82218601208   A
13  1.46976809915   A
14  1.78802276311   A
15  1.93459128836   A
16  1.95665864564   A
17  1.57026992442   A
18  1.15962402775   A
19  1.05305484021   A
20  0.919362594185  A
21  0.833060897559  A
22  0.77778822023   A
23  0.980084775745  A
24  1.32114351777   A
25  1.55352963275   A
26  1.57375922815   A
27  1.14493868782   A
28  0.25294849907   A
29  -0.40599118604  A
30  -0.487054890978 A
31  -0.333389189047 A
32  -0.226405253731 A
33  -0.24558780059  A
34  -0.180403027022 A
35  -0.266733706191 A
36  -0.0762920840723    A
37  0.465100892866  A
38  0.516633798421  A
39  0.644986315681  A
40  1.09115362242   A
41  1.08889196437   A
42  0.862434726048  A
43  0.604042272774  A
44  0.328584834197  A
45  0.598617257523  A
46  1.05219653601   A
47  1.10798332527   A
48  0.948151198722  A
49  0.546516443068  A
50  0.291735961134  A
51  0.238335006253  A
52  0.425304707962  A
53  0.817302425729  A
54  1.38852220304   A
55  2.34024990348   A
56  3.09941186364   A
57  4.06854366458   A
58  4.82115051043   A
59  4.55199542056   A
60  6.17279510607   A
61  10.3162999798   A
62  12.996627449    A
63  12.2731258622   A
64  10.8544867366   A
65  8.27264346102   A
66  5.79180739043   A
67  4.81947524098   A
68  4.19372954801   A
69  3.46244417879   A
70  2.69421581749   A
71  1.93753362259   A
72  1.54011797412   A
73  1.29959330498   A
74  1.21705330118   A
75  1.22382555287   A
76  0.952475753311  A
77  0.686398680367  A
78  0.747237736846  A
79  0.720306209509  A
80  0.463068694886  A
81  0.306876218733  A
82  0.121617637342  A
83  0.0460331524847 A
84  -0.0107323735891    A
85  -0.0629997057801    A
86  -0.19204582316  A
87  -0.371375773255 A
88  -0.42668686684  A
89  -0.326517894916 A
90  -0.277301361031 A
91  -0.0386177879973    A
92  0.0101084138435 A
93  0.0605269021344 A
94  0.182751080385  A
95  0.13310933252   A
96  0.27074048104   A
97  0.267598260699  A
98  0.349363089715  A
99  0.264445135486  A
100 0.218924366626  A
101 0.153338812341  A
102 -0.0679742801523    A
103 -0.30849875963  A
104 -0.28903625474  A
105 -0.188860043325 A
106 -0.204777388005 A
107 -0.1461100225   A
108 0.102433799309  A
109 0.122246106735  A
110 -0.105920831771 A
111 -0.21545039794  A
112 -0.135846098251 A
113 -0.105900152586 A
114 -0.247196889682 A
115 -0.312824264065 A
116 -0.251182865438 A
117 -0.0867113506532    A
118 0.00458490479941    A
119 -0.0960520134953    A
120 -0.235300588181 A
1   0.939235632937  A
2   1.28838263139   A
3   1.42730334901   A
4   1.22224614063   A
5   0.896759827332  A
6   0.642894836093  A
7   0.737029720141  A
8   0.774643396412  A
9   0.759420758029  A
10  1.04696772347   A
11  1.23504525458   A
12  1.0676601916    A
13  0.826029313299  A
14  1.14629521568   A
15  1.14142924359   A
16  1.22956581755   A
17  1.13136892357   A
18  0.800448368445  A
19  0.652191202322  A
20  0.498096263495  A
21  0.555339022027  A
22  0.545965232595  A
23  0.726231857788  A
24  0.849494247969  A
25  0.916259379894  A
26  0.879335232046  A
27  0.56575342831   A
28  0.108604596914  A
29  -0.225555689899 A
30  -0.320456274731 A
31  -0.230459986895 A
32  -0.042388319738 A
33  -0.0833366171628    A
34  -0.0460734786257    A
35  -0.229033279226 A
36  -0.175845833699 A
37  0.197716175342  A
38  0.28980276875   A
39  0.512487189675  A
40  0.683324573043  A
41  0.631659584895  A
42  0.522329104013  A
43  0.393330574908  A
44  0.168841230084  A
45  0.350442790229  A
46  0.72946349718   A
47  0.925052059705  A
48  0.821386076473  A
49  0.505908860228  A
50  0.297370018812  A
51  0.212971428154  A
52  0.390453125173  A
53  0.719985040719  A
54  0.977964675176  A
55  1.54450254277   A
56  2.14621503854   A
57  2.99079642364   A
58  3.39269707733   A
59  3.33188837547   A
60  4.10675880825   A
61  6.96009219664   A
62  9.04165938743   A
63  8.65369320149   A
64  7.94685353567   A
65  5.99410112792   A
66  4.270657622 A
67  3.74053623603   A
68  3.16701121242   A
69  2.34745227622   A
70  1.76409736552   A
71  1.51200803675   A
72  1.2907743594    A
73  1.00681298597   A
74  0.862744443537  A
75  0.91574368888   A
76  0.714689640717  A
77  0.517175945403  A
78  0.567676742354  A
79  0.59107492188   A
80  0.36357410485   A
81  0.136113295885  A
82  -0.0424484841936    A
83  -0.0580144665363    A
84  -0.0982479104419    A
85  -0.125561965887 A
86  -0.18724722966  A
87  -0.319063282063 A
88  -0.310923270725 A
89  -0.297680012209 A
90  -0.29067812137  A
91  -0.153124902802 A
92  -0.0832141989646    A
93  0.0360608269851 A
94  0.0692223913598 A
95  0.0301088137407 A
96  0.229967884645  A
97  0.286834318788  A
98  0.302023175627  A
99  0.172030225713  A
100 0.128331231506  A
101 0.0852383292109 A
102 -0.0890769934766    A
103 -0.28596925454  A
104 -0.277955689998 A
105 -0.213135107915 A
106 -0.187743795588 A
107 -0.156312203071 A
108 0.00927423989462    A
109 0.0950491919392 A
110 -0.103823712283 A
111 -0.263438354304 A
112 -0.169133590325 A
113 -0.119342668528 A
114 -0.184209907576 A
115 -0.153083100597 A
116 -0.118314865514 A
117 -0.0218234673043    A
118 0.0354090403385 A
119 -0.176859459446 A
120 -0.254330750514 A
1   1.31156238699   B
2   1.66603897664   B
3   1.8595569523    B
4   1.47610814343   B
5   1.13938772251   B
6   1.07959295698   B
7   1.0562167754    B
8   0.953732152873  B
9   1.27923353158   B
10  1.87416928486   B
11  2.29643917738   B
12  2.11874255833   B
13  1.81800847267   B
14  1.97156297894   B
15  1.95639491025   B
16  1.75903105961   B
17  1.36979841803   B
18  1.20025438569   B
19  1.15465650184   B
20  1.09201899355   B
21  0.948241309108  B
22  0.755764015696  B
23  0.89321992313   B
24  1.55401151175   B
25  1.7724765184    B
26  1.61741216053   B
27  1.19119499499   B
28  0.379190890768  B
29  -0.280643671284 B
30  -0.438517977457 B
31  -0.358544058104 B
32  -0.175439246148 B
33  -0.152975829581 B
34  -0.161103632796 B
35  -0.174444281478 B
36  0.0432634194416 B
37  0.426620630846  B
38  0.484334073737  B
39  0.619581343298  B
40  0.967283510405  B
41  1.15176486771   B
42  0.966602160933  B
43  0.690373835041  B
44  0.319762485659  B
45  0.558607945261  B
46  1.11704365618   B
47  1.35119752184   B
48  1.086453978 B
49  0.522235623898  B
50  0.331232373297  B
51  0.470526554506  B
52  0.88872478677   B
53  1.3777468901    B
54  1.98052619207   B
55  2.74167480929   B
56  3.71564209846   B
57  4.7554986573    B
58  5.35724571871   B
59  5.09994377564   B
60  6.79713731723   B
61  13.5623123968   B
62  19.9726094303   B
63  20.6985773902   B
64  19.5622430224   B
65  16.9252890116   B
66  13.785124688    B
67  12.0153484193   B
68  10.6335853944   B
69  9.23145636242   B
70  8.33279506304   B
71  7.11679306668   B
72  5.98971780649   B
73  4.81795605529   B
74  3.6240387853    B
75  3.17710512841   B
76  3.09525364338   B
77  2.94968830182   B
78  2.85812444624   B
79  2.41084230435   B
80  1.80196837641   B
81  1.18822582466   B
82  0.847087211338  B
83  0.844579278397  B
84  0.719435070951  B
85  0.373826290695  B
86  0.179833579104  B
87  0.10827105313   B
88  0.140513871238  B
89  0.36900575791   B
90  0.545788292614  B
91  0.588906392532  B
92  0.411607834074  B
93  0.436297519059  B
94  0.417543346098  B
95  0.420644053229  B
96  0.754192582582  B
97  0.865901044214  B
98  0.821331429891  B
99  0.859522528975  B
100 1.0698784309    B
101 1.12094185211   B
102 0.954696811999  B
103 0.635033784692  B
104 0.593828176146  B
105 0.662067791202  B
106 0.640073276401  B
107 0.773737194106  B
108 1.04778537143   B
109 1.14218831145   B
110 0.936124315428  B
111 0.761897172562  B
112 0.584860054282  B
113 0.568945253284  B
114 0.592650160898  B
115 0.363421418416  B
116 0.315516608971  B
117 0.556218161647  B
118 0.741402531046  B
119 0.73670589581   B
120 0.731201358535  B
1   0.914487112088  B
2   1.17888880951   B
3   1.34882572489   B
4   1.05705037522   B
5   0.767924473683  B
6   0.627155263031  B
7   0.661419743127  B
8   0.726639134719  B
9   0.904404934323  B
10  1.11446499538   B
11  1.32234502189   B
12  1.11272139974   B
13  0.80278277695   B
14  1.05206129918   B
15  1.03054952945   B
16  0.920053144065  B
17  0.890783999963  B
18  0.745241179888  B
19  0.753375762718  B
20  0.606204084282  B
21  0.439550558806  B
22  0.415665737741  B
23  0.511097171088  B
24  0.753958021098  B
25  0.915744174748  B
26  0.914912288237  B
27  0.650537400908  B
28  0.177843412682  B
29  -0.182744261137 B
30  -0.280968150367 B
31  -0.131322561837 B
32  -0.0936594361197    B
33  -0.111213723334 B
34  -0.0751949309223    B
35  -0.114901791545 B
36  -0.0165691620777    B
37  0.117705450621  B
38  0.195665130311  B
39  0.49300993106   B
40  0.748293222013  B
41  0.702261888166  B
42  0.579761929719  B
43  0.354296503405  B
44  0.0685749425124 B
45  0.276465165244  B
46  0.833851789425  B
47  0.952399770341  B
48  0.780124651512  B
49  0.389046185042  B
50  0.240762613037  B
51  0.334216612367  B
52  0.497505803488  B
53  0.759900678942  B
54  1.12736521148   B
55  1.77820644505   B
56  2.60137514  B
57  3.11618984654   B
58  3.21453966104   B
59  3.02507885677   B
60  4.10630457704   B
61  7.81645787291   B
62  11.474225964    B
63  12.4863009071   B
64  11.5994621204   B
65  9.5999931249    B
66  7.96585566708   B
67  7.08309536929   B
68  6.04261032076   B
69  5.26284702902   B
70  4.76880466452   B
71  4.10897764688   B
72  3.33692117069   B
73  2.6513184514    B
74  2.25667303445   B
75  1.83839013124   B
76  1.84439486988   B
77  1.74545691393   B
78  1.60528910561   B
79  1.42843608445   B
80  0.854469270873  B
81  0.563227776699  B
82  0.299704948636  B
83  0.270143019019  B
84  0.283668987216  B
85  0.181017474033  B
86  0.0822636124446 B
87  -0.0465301247043    B
88  -0.000460798489744  B
89  0.0926334491843 B
90  0.192295768771  B
91  0.29542265617   B
92  0.218214141112  B
93  0.145843998014  B
94  0.12500365606   B
95  0.147889577395  B
96  0.368717140352  B
97  0.580075802767  B
98  0.459540843701  B
99  0.459168312255  B
100 0.614213389976  B
101 0.627367541442  B
102 0.562408018057  B
103 0.31716603245   B
104 0.288874098133  B
105 0.303797627692  B
106 0.338691064084  B
107 0.336034553249  B
108 0.587964074115  B
109 0.649227026019  B
110 0.541946830382  B
111 0.542082081996  B
112 0.51192491824   B
113 0.327474693143  B
114 0.254878532604  B
115 0.134544321919  B
116 0.100368014222  B
117 0.211272916527  B
118 0.267339751552  B
119 0.30990753715   B
120 0.444492582364  B

Thanks again!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As @MrFlick already said, you can't calculate a standard error for each x-value. However, there are several option you can consider.

Option 1: plot a loess smooth with a very small span with stat_smooth in which you can include a shaded area for the standard error:

ggplot(dat, aes(x=pos,y=value, colour=type)) +
  stat_smooth(method="loess", span=0.1, se=TRUE, aes(fill=type), alpha=0.3) +
  theme_bw()

this gives: enter image description here

Option 2: as you have a high and a low values for each x value, you can plot seperate lines for the high and low values. You have to creat a high/low variable first:

dat$level <- rep(c("high","low"), each=120)
ggplot(dat, aes(x=pos,y=value, colour=type)) +
  geom_line(aes(linetype=level)) +
  theme_bw()

this gives: enter image description here

Option 3: as you have a high and a low values for each x value, you can plot a geom_ribbon between the high and low value with:

ggplot(dat, aes(x=pos,y=value, colour=type)) +
  stat_summary(geom="ribbon", fun.ymin="min", fun.ymax="max", aes(fill=type), alpha=0.3) +
  theme_bw()

this gives: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...