0

I am using this code to plot my dataframe. The statistics variable contains two values: "mean" and "sd".

ggplot(NDVIdf_forplot, aes(x = statistic, y= value, group = ID)) + geom_line()

If I use that code the graph includes both the "mean" and "sd" categories. I want to use only those observations that are in the "mean" class of the statistic variable and later use the "sd" class to plot geom_errorbar

I used this code before but did not manage to create what I want:

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()

edit ---

The data I want to plot look like this (I'm showing only the top rows). The idea is to use NDVI_mean to create the lines and NDVI_sd to create the error bars on the same graph

> 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

So far this image shows how the plot is being produced. As you can see both NDVI_mean and NDVI_sd are used but this should not be the case. NDVI_sd should be used to produce geom_errorbar

enter image description here

4
  • Please post example of your data Commented Oct 30, 2017 at 10:55
  • @PoGibas, I have updated the question to show you the data. Thanks for your help Commented Oct 30, 2017 at 12:39
  • and what is valuesdf? Commented Oct 30, 2017 at 12:39
  • My mistake, that is a code of an older version that is not useful anymore since the data structure changed. Maybe I should remove it but I included it to show what I was trying to do before Commented Oct 30, 2017 at 12:41

1 Answer 1

2

Code:

# 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))

Result plot:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. What if I want to plot on the Y axes the values of NDVI_mean, on the x axes the NDVI_mean.1, NDVI_mean.2 etc. and then one line for each ID (1 to 10)
@GCGM I can't image what kind of figure is that
Let me give you more details about my data. I have 10 images (multi-temporal dataset). For each image, I am deriving an index and then extracting the value of this index at my observation locations (10 points). What I want to produce is a graph where the value of that index is on the Y axes, the image date (image1, image2, etc) is on the X axes. So I would have one line for each observation (point). In my data, NDVI_mean is related to NDVI_sd (meaning those are one image), NDVI_mean.1 and NDVI_sd.1 are another image and so on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.