我正在使用此代码绘制我的数据帧。统计变量包含两个值:“mean”和“sd”。
ggplot(NDVIdf_forplot, aes(x = statistic, y= value, group = ID)) + geom_line()
如果我使用该代码,该图表包括“均值”和“sd”类别。我想只使用统计变量的“均值”类中的那些观察结果,然后使用“sd”类来绘制geom_errorbar
之前我使用过这段代码,但没有设法创建我想要的东西:
ggplot(NDVIdf_forplot,aes(x=mean,y=value))+geom_errorbar(aes(ymin=NDVI_mean-NDVI_sd, ymax=NDVI_mean+NDVI_sd), width=0.1)+geom_line()+geom_point()
编辑---
我要绘制的数据看起来像这样(我只显示顶行)。我们的想法是使用NDVI_mean创建线条,使用NDVI_sd在同一图形上创建误差线
> NDVIdf_forplot
ID statistic value
1 1 NDVI_mean 0.052957208
2 2 NDVI_mean 0.044501794
3 3 NDVI_mean 0.077902512
4 4 NDVI_mean 0.141576609
5 5 NDVI_mean 0.653835647
6 6 NDVI_mean 0.716164870
7 7 NDVI_mean 0.386612348
8 8 NDVI_mean 0.486527816
9 9 NDVI_mean 0.226190208
10 10 NDVI_mean 0.573239754
11 1 NDVI_sd 0.008259909
12 2 NDVI_sd 0.015453091
13 3 NDVI_sd 0.099944407
14 4 NDVI_sd 0.091479545
15 5 NDVI_sd 0.223150965
16 6 NDVI_sd 0.074045394
17 7 NDVI_sd 0.058177949
18 8 NDVI_sd 0.109762451
19 9 NDVI_sd 0.019822312
20 10 NDVI_sd 0.104795771
21 1 NDVI_mean.1 0.081417705
22 2 NDVI_mean.1 0.036114126
23 3 NDVI_mean.1 0.037729680
24 4 NDVI_mean.1 0.016398037
25 5 NDVI_mean.1 0.052672604
26 6 NDVI_mean.1 0.024580946
27 7 NDVI_mean.1 0.064811390
28 8 NDVI_mean.1 0.119724256
29 9 NDVI_mean.1 0.078961665
30 10 NDVI_mean.1 0.041025489
31 1 NDVI_sd.1 0.016093458
32 2 NDVI_sd.1 0.027927592
33 3 NDVI_sd.1 0.046937888
34 4 NDVI_sd.1 0.011805721
35 5 NDVI_sd.1 0.026467984
36 6 NDVI_sd.1 0.028896611
37 7 NDVI_sd.1 0.016313583
38 8 NDVI_sd.1 0.066647683
39 9 NDVI_sd.1 0.022800589
40 10 NDVI_sd.1 0.015085673
41 1 NDVI_mean.2 0.063375514
42 2 NDVI_mean.2 0.086191853
43 3 NDVI_mean.2 0.092580942
44 4 NDVI_mean.2 0.144053635
45 5 NDVI_mean.2 0.696155509
46 6 NDVI_mean.2 0.252707792
47 7 NDVI_mean.2 0.144636380
48 8 NDVI_mean.2 0.757321462
49 9 NDVI_mean.2 0.689617575
50 10 NDVI_mean.2 0.179591653
51 1 NDVI_sd.2 0.010017152
52 2 NDVI_sd.2 0.023206464
53 3 NDVI_sd.2 0.106580902
54 4 NDVI_sd.2 0.097440674
55 5 NDVI_sd.2 0.231063744
56 6 NDVI_sd.2 0.043961963
57 7 NDVI_sd.2 0.010335935
58 8 NDVI_sd.2 0.061841114
59 9 NDVI_sd.2 0.048363788
60 10 NDVI_sd.2 0.111704779
61 1 NDVI_mean.3 0.048932939
62 2 NDVI_mean.3 0.110942174
63 3 NDVI_mean.3 0.080362752
64 4 NDVI_mean.3 0.132868790
65 5 NDVI_mean.3 0.682639604
66 6 NDVI_mean.3 0.503766225
67 7 NDVI_mean.3 0.120794820
68 8 NDVI_mean.3 0.777808416
69 9 NDVI_mean.3 0.755741184
70 10 NDVI_mean.3 0.058089687
71 1 NDVI_sd.3 0.009048781
72 2 NDVI_sd.3 0.029528930
73 3 NDVI_sd.3 0.098454753
74 4 NDVI_sd.3 0.089512544
75 5 NDVI_sd.3 0.241257647
76 6 NDVI_sd.3 0.114466677
77 7 NDVI_sd.3 0.013347437
78 8 NDVI_sd.3 0.066441491
79 9 NDVI_sd.3 0.065787691
80 10 NDVI_sd.3 0.013351357
到目前为止,这张图片展示了如何制作情节。如您所见,NDVI_mean和NDVI_sd都被使用,但情况并非如此。 NDVI_sd应该用于生成geom_errorbar
答案 0 :(得分:2)
代码:
# Transform data
# Here we make table with three columns (ID Mean SD)
pd <- reshape2::dcast(NDVIdf_forplot, ID ~ statistic, value.var = "value")
# Plot data using ggplot2
library(ggplot2)
ggplot(pd, aes(ID, NDVI_mean)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = NDVI_mean - NDVI_sd,
ymax = NDVI_mean + NDVI_sd))
结果图: